标签:
传送门:http://poj.org/problem?id=1860
题目:
Time Limit:1000MS | Memory Limit:30000KB | 64bit IO Format:%I64d & %I64u |
Description
Input
Output
Sample Input
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
Sample Output
YES
思路:若涨钱,则一定存在正环;若存在正环,则钱可以不停的增加,故可以涨钱;所以用bellman_ford判环,但初始化为0,求最长路;
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<stack> #include<map> #define INF 0x3f3f3f3f #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long LL; const int N=100007; struct Edge{ int u,v; double ra,co; Edge(int a=0,int b=0,double c=0,double d=0):u(a),v(b),ra(c),co(d){} }; vector<Edge> E; double dis[N]; bool bellman_ford(int s,double v,int n) { for(int i=1;i<=n;i++) dis[i]=0; dis[s]=v; //relax for(int i=1;i<=n-1;i++) { bool flag=false; for(int j=0;j<E.size();j++) { if(dis[E[j].v]<(dis[E[j].u]-E[j].co)*E[j].ra) { dis[E[j].v]=(dis[E[j].u]-E[j].co)*E[j].ra; flag=true; } } if(!flag) return false; } //Search Positive Circle for(int i=0;i<E.size();i++) { if(dis[E[i].v]<(dis[E[i].u]-E[i].co)*E[i].ra) return true; } return false; } int main() { int n,m,s; double v; while(scanf("%d%d%d%lf",&n,&m,&s,&v)==4) { E.clear(); int a,b; double ra,ca,rb,cb; for(int i=0;i<m;i++) { scanf("%d%d%lf%lf%lf%lf",&a,&b,&ra,&ca,&rb,&cb); E.push_back(Edge(a,b,ra,ca)); E.push_back(Edge(b,a,rb,cb)); } if(bellman_ford(s,v,n)) printf("YES\n"); else printf("NO\n"); } return 0; }
POJ 1860 Currency Exchange [bellman_ford]
标签:
原文地址:http://blog.csdn.net/code_or_code/article/details/43712491