http://poj.org/problem?id=1860

题意:  题目中主要是说存在货币兑换点,然后现在手里有一种货币,要各种换来换去,最后再换回去的时候看能不能使原本的钱数增多,每一种货币都有对应的汇率,而货币A到货币B的汇率即为1货币A换得得货币B的数量,但兑换点是要收取佣金的,且佣金从源货币中扣除,例如,你想在汇率29.75,佣金为0.39的兑换点把100美元换成卢布,得到的卢布数即为(100-0.39)*29.75 = 2963.3975.

样例解释:

3 2 1 20.0                          
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00

   多组输入,第一行中N代表有N种货币可以互相兑换,M代表有M个货币兑换点,S代表这个人手中的的货币的编号,V代表这个人手中拥有的货币数量,底下M行

每行六个数,A,B代表可以交换的货币A和B,剩下的实数RAB,CAB,RBA,CBA,代表A到B的汇率,佣金,B到A的汇率,佣金。以某种兑换方式增加原本的钱数,而且必须兑换为原来的货币。

解法:用spfa和Bellman都可以,我用的是spfa,改了一下原模板就过了

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 int u,v,w;
 8 const int maxn = 1011;
 9 const int maxm = 10011;
10 const int oo = 1<<29;
11 struct node
12 {
13     int u;
14     int v;
15     double x,y ;
16     int next;
17 }edge[maxm];
18 double dis[maxn];
19 int m,n,num;
20 double ount;
21 int head[maxn],cnt,sum[maxn];
22 int vis[maxn] = {0};
23 queue<int>qu;
24 void add(int u,int v,double x,double y)
25 {
26     edge[cnt].u = u ;
27     edge[cnt].v = v ;
28     edge[cnt].x = x ;
29     edge[cnt].y = y ;
30     edge[cnt].next = head[u];
31     head[u] = cnt++ ;
32 }
33 int spfa(int s)
34 {
35     for(int i = 0 ; i < m ; i++)
36     {
37         dis[i] = 0;
38         vis[i] = 0 ;
39     }
40     dis[s] = ount;
41     qu.push(s);
42     vis[s] = 1 ;
43     while(!qu.empty())
44     {
45         int u = qu.front();
46         qu.pop();
47         vis[u] = 0;
48         for(int i = head[u] ; i != -1 ; i = edge[i].next)
49         {
50             int v = edge[i].v;
51             if((dis[u]-edge[i].y)*edge[i].x > dis[v])
52             {
53                 dis[v] = (dis[u]-edge[i].y)*edge[i].x;
54                 if(!vis[v])
55                 {
56                     vis[v] = 1;
57                     qu.push(v);
58                 }
59                 sum[v]++;
60                 if(sum[v] > m)
61                 return -1;
62             }
63         }
64     }
65     return 1;
66 }
67 void Init()
68 {
69     cnt = 0 ;
70     memset(head,-1,sizeof(head));
71     memset(sum,0,sizeof(sum));
72     while(!qu.empty())
73     qu.pop();
74 }
75 int main()
76 {
77     while(scanf("%d %d %d %lf",&m,&n,&num,&ount)!=EOF)
78     {
79         Init();
80         int u,v;
81         double x,y,xx,yy ;
82         for(int i = 0 ; i < n ; i++)
83         {
84             scanf("%d %d %lf %lf %lf %lf",&u,&v,&x,&y,&xx,&yy);
85             add(u,v,x,y);
86             add(v,u,xx,yy);
87         }
88         if(spfa(num) > 0)
89         cout<<"NO"<<endl;
90         else cout<<"YES"<<endl;
91     }
92     return 0;
93 }
View Code

 http://blog.csdn.net/lyy289065406/article/details/6645778

这个大神用的是Bellman

posted on 2013-08-13 17:17  枫、  阅读(252)  评论(0)    收藏  举报