您的位置:首页 > 其它

poj 1797 一条路径中的最小边 再找出最大的

2015-06-21 21:13 267 查看

Sample Input

1 // T
3 3// n m
1 2 3//u v w
1 3 4
2 3 5
Sample Output

Scenario #1:
4

 

# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ;

const int MAXN=1010;
const int INF=0x3f3f3f3f;
int cost[MAXN][MAXN];
int lowcost[MAXN];
bool vis[MAXN];
int n ;

void Dijkstra(int beg)
{
for(int i=1;i<=n;i++)
{
lowcost[i]=0;
vis[i]=false;
}
lowcost[beg]=INF;
for(int j=0;j<n;j++)
{
int k=-1;
int Max=0;
for(int i=1;i<=n;i++)
if(!vis[i]&&lowcost[i]>Max)
{
Max=lowcost[i];
k=i;
}
if(k==-1)break;
vis[k]=true;
for(int i=1;i<=n;i++)
if(!vis[i]&&min(lowcost[k],cost[k][i])>lowcost[i] )
lowcost[i]=min(lowcost[k],cost[k][i]);
}
}

int main()
{
//  freopen("in.txt","r",stdin) ;
int T;
int m;
scanf("%d",&T);
int iCase=0;
while(T--)
{
iCase++;
scanf("%d%d",&n,&m);
memset(cost,0,sizeof(cost));
int u,v,w;
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
cost[u][v]=cost[v][u]=max(cost[u][v],w);
}
Dijkstra(1);
printf("Scenario #%d:\n",iCase);
printf("%d\n\n",lowcost
);
}
return 0;
}
View Code

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: