九度oj 1545奇怪的連通圖

題目1545:奇怪的連通圖

時間限制:1 秒

內存限制:128 兆

特殊判題:

提交:164

解決:44

題目描述:

已知一個無向帶權圖,求最小整數k。使僅使用權值小於等於k的邊,節點1可以與節點n連通。

輸入:

輸入包含多組測試用例,每組測試用例的開頭爲一個整數n(1 <= n <= 10000),m(1 <= m <= 100000),代表該帶權圖的頂點個數,和邊的個數。
接下去m行,描述圖上邊的信息,包括三個整數,a(1 <= a <= n),b(1 <= b <= n),c(1 <= c <= 1000000),表示連接頂點a和頂點b的無向邊,其權值爲c。

輸出:

輸出爲一個整數k,若找不到一個整數滿足條件,則輸出-1。

樣例輸入:
3 3
1 3 5
1 2 3
2 3 2
3 2
1 2 3
2 3 5
3 1
1 2 3 
樣例輸出:
3
5
-1
比賽的時候沒想出來。

很笨地用DFS遞歸枚舉。。

然後果斷超時。。

一個數據都沒過。。

然後第二天早上起來果斷用BFS寫了一下。。

wa~~

有點小激動,因爲沒有超時。

然後將隊列改爲優先隊列果斷A了

yes!!


#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
struct node
{
    int ne;
    int va;
};
struct node1
{
	int next;
	int m;
	bool operator < (const node1& a) const
	{
		return m>a.m;
	}
};
vector<node> map[10002];
int visit[10002];
int n,t,in;
int mmin(int aa,int bb)
{
    return aa<bb?aa:bb;
}
int max(int aa,int bb)
{
    return aa>bb?aa:bb;
}

int BFS()
{
	int xiao = 1000008;
	priority_queue<node1> qu;
	struct node1 needru,needpush,var;
	needru.next = 1;
	needru.m = 0;
	qu.push(needru);
	visit[1] = 1;
	while(!qu.empty())
	{
		needpush = qu.top();
		qu.pop();
		if(needpush.next==n)
			xiao = mmin(xiao,needpush.m);
		int len = map[needpush.next].size();
		visit[needpush.next] = 1;
		for(int i=0;i<len;i++)
		{
			if(!visit[map[needpush.next][i].ne])
			{
				var.next = map[needpush.next][i].ne;
				var.m = max(map[needpush.next][i].va,needpush.m);
				qu.push(var);
				
			}
		}
	}
	return xiao;

}
int main()
{
    int i,j,a,b;
    struct node qq;
    while(scanf("%d%d",&n,&t)!=EOF)
    {
        in = 1000008;
        memset(visit,0,sizeof(visit));
        for(i=0;i<t;i++)
        {
            scanf("%d%d%d",&a,&b,&j);
            qq.ne = b;
            qq.va = j;
            map[a].push_back(qq);
            qq.ne = a;
            map[b].push_back(qq);
        }
        visit[1] = 1;
		in = BFS();
        if(in!=1000008)
        printf("%d\n",in);
        else
            printf("-1\n");
        for(i=0;i<=n;i++)
        map[i].clear();
    }
 
    return 0;
}




發佈了58 篇原創文章 · 獲贊 89 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章