标签:
题目链接:http://acm.swust.edu.cn/problem/0582/
4 4
1 2 3
2 4 10
1 3 5
3 4 5
1 4
|
10 |
1 #include <iostream> 2 #include <queue> 3 #include <vector> 4 #include <cstring> 5 using namespace std; 6 7 #define maxn 10005 8 #define inf 0x3f3f3f3f 9 10 struct node{ 11 int v, w; 12 node(int x, int y) :v(x), w(y){}; 13 node(){}; 14 }; 15 16 vector<node>mpt[maxn]; 17 //ptr判断一个点的入队操作次数,判断负环 18 int vis[maxn], ptr[maxn], dis[maxn], t, n, m, u, v, w, s; 19 20 int Spfa(int s){ 21 queue<int>Q; 22 Q.push(s); 23 vis[s] = 1, dis[s] = 0, ptr[s] = 1; 24 while (!Q.empty()){ 25 int u = Q.front(); 26 Q.pop(); 27 vis[u] = 0; 28 for (int i = 0; i<mpt[u].size(); i++){ 29 v = mpt[u][i].v; 30 if (dis[v]>dis[u] + mpt[u][i].w){ 31 dis[v] = dis[u] + mpt[u][i].w; 32 if (!vis[v]){ 33 vis[v] = 1; 34 Q.push(v); 35 if (++ptr[v] > n) return 0; 36 } 37 } 38 } 39 } 40 return 1; 41 } 42 43 int main(){ 44 while (cin >> n >> m){ 45 memset(vis, 0, sizeof(vis)); 46 memset(dis, inf, sizeof(dis)); 47 memset(ptr, 0, sizeof(ptr)); 48 for (int i = 0; i < m; i++){ 49 cin >> u >> v >> w; 50 mpt[u].push_back(node(v, w)); 51 } 52 cin >> s >> t; 53 if (Spfa(s)) 54 cout << dis[t] << endl; 55 else 56 cout << "No Solution!\n"; 57 } 58 return 0; 59 }
[Swust OJ 582]--放学了,抢机子了(SPFA)
标签:
原文地址:http://www.cnblogs.com/zyxStar/p/4605607.html