HDU 1217 Arbitrage(最短路spfa判环)
2016-09-27 18:39
369 查看
#include <map> #include <queue> #include <string> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 35; string str1, str2; map <string, int> M; //将字符串映射成数字 int head[maxn], tot, n, m; double w; struct Edge{ //邻接表 int to, next; double w; }e[10005]; void addedge(int from, int to, double w){ e[tot].to = to; e[tot].w = w; e[tot].next = head[from]; head[from] = tot++; } bool spfa(int s){ bool vis[maxn]; //判断是否走过 int cnt[maxn]; //判断走了多少次 double d[maxn]; //相当于找最短路径,这里面其实就是兑换最大比例 memset(vis, false, sizeof(vis)); memset(cnt, 0, sizeof(cnt)); memset(d, 0, sizeof(d)); queue <int> que; que.push(s); d[s] = 1.0; vis[s] = true; while(!que.empty()){ int u = que.front(); que.pop(); vis[u] = false; for(int i = head[u]; i != -1; i = e[i].next){ int v = e[i].to; if(d[v] < d[u]*e[i].w){ //判断条件变了,因为要找最大的兑换比例 d[v] = d[u]*e[i].w; if(!vis[v]){ vis[v] = true; que.push(v); if(++cnt[v] >= n) return true; } } } } return false; } void init(){ memset(head, -1, sizeof(head)); tot = 0; M.clear(); } int main(){ int kase = 0; while(scanf("%d", &n) != EOF && n){ init(); for(int i = 1; i <= n; i++){ cin >> str1; M.insert(make_pair(str1, i)); } scanf("%d", &m); for(int i = 1; i <= m; i++){ cin >> str1 >> w >> str2; addedge(M[str1], M[str2], w); } int flag = 0; for(int i = 1; i <= n; i++) if(spfa(i)){flag = 1; break;} printf("Case %d: ", ++kase); if(flag) puts("Yes"); else puts("No"); } return 0; }
相关文章推荐
- HDU 1217 Arbitrage 【最短路,map+spfa】
- HDU 1217 Arbitrage (SPFA bellman-ford flyod )
- HDU 1217 Arbitrage(最短路判断负环)
- 【map && spfa求正权回路】HDU - 1217 Arbitrage
- [ACM] hdu 1217 Arbitrage (bellman_ford最短路,判断是否有正权回路或Floyed)
- HDU - 1217 Arbitrage [最短路]
- 文章标题 HDU 1217 : Arbitrage(最短路--Floyd+map)
- HDU1217-Arbitrage(乘法最短路)
- 【最短路+floyd】杭电 hdu 1217 Arbitrage
- hdu 1217 Arbitrage(最短路--floyd+对数的性质)
- [ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)
- HDU 1217 Arbitrage 乘法最短路
- hdu 1217 Arbitrage 最短路 floyd+map容器
- hdu 1217 Arbitrage--Floyd
- POJ 2240 HDU 1217 Arbitrage
- hdu 5418 Victor and World 状态压缩+SPFA最短路
- hdu 2544 最短路 spfa
- hdu 2544 最短路 (spfa)
- hdu:4360(spfa)求各种状态的最短路
- hdu 0 or 1(最短路spfa)(本质,抽象)