2017年3月25日 | ljfcnyali
题目大意
给定N种货币,某些货币之间可以相互兑换,现在给定一些兑换规则,问能否从某一种货币开始兑换,经过一些中间货币之后,最后兑换回这种货币,并且得到的钱比之前的多。
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
1
YES
题目分析
使用SPFA判断负权环,一个一个乱搞就可以了。
AC代码
/*************************************************************************
> File Name: POJ1860.cpp
> Author: ljf-cnyali
> Mail: ljfcnyali@gmail.com
> Created Time: 2017/3/25 15:00:46
************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>
using namespace std;
#define REP(i, a, b) for(int i = (a), _end_ = (b);i <= _end_; ++ i)
#define mem(a) memset((a), 0, sizeof(a))
#define str(a) strlen(a)
const int maxn = 210;
int n, m, S;
int begin[maxn], next[maxn], to[maxn], e, vis[maxn], Num[maxn];
double V, r[maxn], c[maxn], dist[maxn];
void add(int x, int y, double a, double b) {
to[++ e] = y;
next[e] = begin[x];
begin[x] = e;
r[e] = a;
c[e] = b;
}
bool SPFA() {
queue<int> Q;
vis[S] = 1;
dist[S] = V;
Q.push(S);
Num[S] ++;
while(!Q.empty()) {
int p = Q.front();
Q.pop();
vis[p] = 0;
for(int i = begin[p]; i; i = next[i]) {
int q = to[i];
if(dist[q] < (dist[p] - c[i]) * r[i]) {
dist[q] = (dist[p] - c[i]) * r[i];
if(!vis[q]) {
vis[q] = 1;
Q.push(q);
Num[q] ++;
if(Num[q] > n)
return true;
}
}
}
}
if(dist[S] > V)
return true;
return false;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int x, y;
double r1, c1, r2, c2;
while(scanf("%d%d%d%lf", &n, &m, &S, &V) != EOF) {
e = 0;
mem(begin);mem(next);mem(to);mem(vis);mem(r);mem(c);
REP(i, 1, m) {
scanf("%d%d%lf%lf%lf%lf", &x, &y, &r1, &c1, &r2, &c2);
add(x, y, r1, c1);
add(y, x, r2, c2);
}
if(SPFA())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}