Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 39754 | Accepted: 14602 |
Description
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.
As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .
To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.
Input
Output
Sample Input
2 3 3 1 1 2 2 1 3 4 2 3 1 3 1 3 3 2 1 1 2 3 2 3 4 3 1 8
Sample Output
NO YES
Hint
#include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cstdlib> #include <string> #include <vector> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> using namespace std; #define INF 0x3f3f3f3 const int N=100005; const int mod=1e9+7; int f,n,m,w; int d[505]; struct Edge{ int u,v,cost; }edge[N]; bool bellman_ford(){ for (int i=1; i<n; i++) { for (int j=1; j<=2*m+w; j++) { if (d[edge[j].v]>d[edge[j].u]+edge[j].cost) { d[edge[j].v]=d[edge[j].u]+edge[j].cost; } } } bool flag=true; for (int j=1; j<=2*m+w; j++) if (d[edge[j].v]>d[edge[j].u]+edge[j].cost) { flag=false; break; } return flag; } int main(){ int s,e,t; cin>>f; while (f--) { cin>>n>>m>>w; memset(d, INF, sizeof(d)); for (int i=1; i<=2*m; i+=2) { scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].cost); edge[i+1].u=edge[i].v; edge[i+1].v=edge[i].u; edge[i+1].cost=edge[i].cost; } for (int i=1; i<=w; i++) { scanf("%d %d %d",&edge[i+2*m].u,&edge[i+2*m].v,&edge[i+2*m].cost); edge[i+2*m].cost*=-1; } if (!bellman_ford()) { cout<<"YES\n"; } else { cout<<"NO\n"; } } return 0; }
#include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cstdlib> #include <string> #include <vector> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> using namespace std; #define INF 0x3f3f3f3 const int N=100005; const int mod=1e9+7; int f,n,m,w; int d[505],cnt[505]; vector<pair<int, int> > a[505]; bool in_queue[505]; queue<int> q; bool SPFA(int st){ memset(d, 0x3f, sizeof(d)); memset(in_queue, 0, sizeof(in_queue)); memset(cnt, 0, sizeof(cnt)); d[st]=0; q.push(st); in_queue[st]=1; cnt[st]=1; while (!q.empty()) { int u=q.front(); if (cnt[u]>n) { return false; } for (int i=0; i<a[u].size(); i++) { pair<int, int> v=a[u][i]; if (d[u]+v.second<d[v.first]) { d[v.first]=d[u]+v.second; if (!in_queue[v.first]) { q.push(v.first); in_queue[v.first]=1; cnt[v.first]++; } } } q.pop(); in_queue[u]=0; } return true; } int main(){ int s,e,t; cin>>f; while (f--) { cin>>n>>m>>w; while (!q.empty()) { q.pop(); } for (int i=0; i<=n; i++) { a[i].clear(); } for (int i=1; i<=m; i++) { scanf("%d %d %d",&s,&e,&t); a[s].push_back(make_pair(e, t)); a[e].push_back(make_pair(s, t)); } for (int i=1; i<=w; i++) { scanf("%d %d %d",&s,&e,&t); a[s].push_back(make_pair(e, -t)); } int flag=1; for (int i=1; i<=n; i++) { if (!SPFA(i)) { flag=0; break; } } if (flag) { cout<<"NO\n"; } else { cout<<"YES\n"; } } return 0; }