其他分享
首页 > 其他分享> > Currency Exchange POJ - 1860

Currency Exchange POJ - 1860

作者:互联网

原题链接

考察:spfa或者BF

本题边有多个属性值,所以需要多开几个数组

正确思路:

      因为图是成环形的,最终会回到原点,如果值变小了最后队列会为empty,如果值变大了,队列会在i==s时,跳出循环

本题最好重新做过一遍

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #include <algorithm>
 6 using namespace std;
 7 const int N = 110;
 8 int n,m,s;
 9 double v,g[N][N],d[N][N],dist[N];
10 bool st[N];
11 bool spfa(int s)
12 {
13     queue<int> q;
14     q.push(s);
15     dist[s] = v;
16     while(q.size())
17     {
18         int t = q.front();
19         st[t] = 0;
20         q.pop();
21         for(int i=1;i<=n;i++)
22         {
23             if(dist[i]<(dist[t]-d[t][i])*g[t][i])
24             {
25                 dist[i] = (dist[t]-d[t][i])*g[t][i];
26                 if(i==s) return true;
27                 if(!st[i]) q.push(i),st[i] = 1;
28             }
29         }
30     }
31     return false;
32 }
33 int main()
34 {
35 //    freopen("in.txt","r",stdin);
36     scanf("%d%d%d%lf",&n,&m,&s,&v);
37     for(int i=1;i<=n;i++) g[i][i] =1;
38     for(int i=1;i<=m;i++)
39     {
40         int x,y; double toy1,toy2,tox1,tox2;
41         scanf("%d%d%lf%lf%lf%lf",&x,&y,&toy1,&toy2,&tox1,&tox2);
42         g[x][y] = toy1,g[y][x] = tox1;
43         d[x][y] = toy2,d[y][x] = tox2;
44     }
45     if(spfa(s)) puts("YES");
46     else puts("NO");
47     return 0;
48 }

 

标签:dist,1860,int,st,Currency,spfa,POJ,bool,include
来源: https://www.cnblogs.com/newblg/p/14304873.html