POJ T1797 Heavy Transportation
题目思路:
因为给的是边,所以就没犹豫用了Spfa,模板裸题.......
Spfa写法
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#define maxn 1005
#define INF 0x3f3f3f3f
using namespace std;
bool vis[maxn];
int dis[maxn],head[maxn],n,m,cnt;
struct Edge{
int to,w,next;
}edge[maxn*maxn];
void Init(){
cnt = 0;
for(int i = 1; i <= n; i++){
vis[i] = false;
dis[i] = 0;
head[i] = -1;
}
}
void add(int from,int to,int w){
edge[cnt].to = to;
edge[cnt].w = w;
edge[cnt].next = head[from];
head[from] = cnt++;
}
void Spfa(int s){
int u;
queue<int> Q;
Q.push(s);
vis[s] = true;
dis[s] = INF;
while(!Q.empty()){
u = Q.front();
Q.pop(); //注意出队列
vis[u] = false;
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].to; //
int w = edge[i].w; //这两处用i是指与U相连的边
if(dis[v] < min(dis[u],w)){
dis[v] = min(dis[u],w);
if(!vis[v]){
Q.push(v);
vis[v] = true;
}
}
}
}
}
int main(){
int u,v,w,t,kcase = 0;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
Init();
for(int i = 0; i < m; i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
Spfa(1);
printf("Scenario #%d:\n",++kcase);
printf("%d\n\n",dis[n]);
}
return 0;
}