hihocoder 1093
题解:spfa裸题目,存在重复的边和环没关系。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
int const inf = 0x7f7f7f7f;
int const N = 100000 + 10;
int const M = 1000000 + 10;
int n,m,s,t,d[N];
struct Edge
{
int from,to,dist;
Edge(){}
Edge(int f,int t,int d):from(f),to(t),dist(d){}
};
vector<Edge>G[N];
queue<int>q;
int spfa(int s){
for(int i=1;i<=n;i++) d[i] = inf;
int vis[N];
memset(vis,0,sizeof(vis));
d[s] = 0;
q.push(s);
while(!q.empty()){
int u = q.front(); q.pop();
vis[u] = false;
for(int i=0;i<G[u].size();i++){
Edge e = G[u][i];
if(d[e.to] > d[e.from] + e.dist){
d[e.to] = d[e.from] + e.dist;
if(!vis[e.to]){
vis[e.to] = true;
q.push(e.to);
}
}
}
}
return d[t];
}
int main(){
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1;i<=m;i++){
int from,to,dist;
scanf("%d%d%d",&from,&to,&dist);
G[from].push_back(Edge(from,to,dist));
G[to].push_back(Edge(to,from,dist));
}
printf("%d\n",spfa(s));
return 0;
}