本身很简单的spfa判环 TLE了一把是因为没写map(不会)
看着别人的答案临时学了一发发现只是用的话还是挺简单的 (但是绝对别学别人直接命名为m) 800多MS水过
噢对了这题Pending到超时了三次 POI绝对有毒
#include <iostream> #include <cstring> #include <string> #include <queue> #include <map> #include <algorithm> using namespace std; map<string, int> mapp; double val[40][40]; int n, m; bool spfa(int s) { double dis[40]; bool vis[40]; int time[40]; memset(dis, 0, sizeof dis); memset(vis, 0, sizeof vis); memset(time, 0, sizeof time); queue<int> q; dis[s] = 1.0; vis[s] = true; q.push(s); while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = false; for(int i = 1; i <= n; i++){ if(dis[i] < dis[u] * val[u][i]){ dis[i] = dis[u] * val[u][i]; if(!vis[i]){ vis[i] = true; q.push(i); if(++time[i] >= n) return true; } } } } return false; } int main() { int kase = 0; while(1){ cin >> n; if(n == 0) break; memset(val, 0, sizeof val); for(int i = 1; i <= n; i++){ string str; cin >> str; mapp[str] = i; } cin >> m; for(int i = 1; i <= m; i++){ string str1, str2; double value; cin >> str1 >> value >> str2; val[mapp[str1]][mapp[str2]] = value; } if(spfa(1)) cout << "Case " << ++kase << ": Yes" << endl; else cout << "Case " << ++kase << ": No" << endl; } return 0; }
kuangbin_ShortPathI (POJ 2240)
原文:http://www.cnblogs.com/quasar/p/5084095.html