标签:最短路
。。。又一个另类OJ。。。最长路 贴个SPFA的
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
using namespace std;
int mp[555][555];
int dis[555];
bool vis[555];
int n,m,tp,s;
void SPFA()
{
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
vis[s] = 1;
queue <int> q;
q.push(s);
vis[s] = 1;
dis[s] = 0;
int u,v,w,i,k;
while(!q.empty())
{
u = q.front();
q.pop();
vis[u] = 0;
for(i = 0; i < n; ++i)
{
if(mp[u][i] == -1) continue;
k = dis[i];
if(dis[i] == -1) dis[i] = max(mp[u][i],dis[u]);
else dis[i] = min(dis[i],max(dis[u],mp[u][i]));
if(k == dis[i]) continue;
if(!vis[i])
{
q.push(i);
vis[i] = 1;
}
}
}
}
int main()
{
int t,u,v,w,i,j;
scanf("%d",&t);
for(j = 1; j <= t; ++j)
{
printf("Case %d:\n",j);
scanf("%d %d",&n,&m);
tp = 0;
memset(mp,-1,sizeof(mp));
for(i = 0; i < m; ++i)
{
scanf("%d %d %d",&u,&v,&w);
mp[u][v] = mp[v][u] = (mp[u][v] == -1)? w: min(w,mp[u][v]);
}
scanf("%d",&s);
SPFA();
for(i = 0; i < n; ++i)
{
if(dis[i] == -1) puts("Impossible");
else printf("%d\n",dis[i]);
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:最短路
原文地址:http://blog.csdn.net/challengerrumble/article/details/47359851