Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9687 | Accepted: 4647 |
Description
Some cows like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows dislike each other and the minimum distance by which they must be separated.
Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance constraints.
Input
Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart.
Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.
Output
Sample Input
4 2 1 1 3 10 2 4 20 2 3 3
Sample Output
27
Hint
There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart.
The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
#include <stdio.h> #include <algorithm> #include <string.h> #include <iostream> #include <stdlib.h> #include <queue> using namespace std; const int M = 20005; const int N = 1005; const int INF = 99999999; struct Edge{ int v,w,next; }edge[M]; int head[N]; int n; bool vis[N]; int time[N],low[N]; int spfa(int s){ queue<int> q; for(int i=1;i<=n;i++){ vis[i] = false; low[i] = INF; time[i] = 0; } low[s] = 0; time[s]++; q.push(s); while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = false; for(int k = head[u];k!=-1;k=edge[k].next){ int v = edge[k].v,w = edge[k].w; if(low[v]>low[u]+w){ low[v] = low[u]+w; if(!vis[v]){ vis[v] = true; q.push(v); if(time[v]++>n) return -1; } } } } if(low[n]==INF) return -2; return low[n]; } void addEdge(int u,int v,int w,int &k){ edge[k].v = v,edge[k].w = w,edge[k].next = head[u],head[u]=k++; } int main() { int ml,md; while(scanf("%d%d%d",&n,&ml,&md)!=EOF){ memset(head,-1,sizeof(head)); int u,v,w; int tot = 0; for(int i=0;i<ml;i++){ scanf("%d%d%d",&u,&v,&w); addEdge(u,v,w,tot); } for(int i=0;i<md;i++){ scanf("%d%d%d",&u,&v,&w); addEdge(v,u,-w,tot); } for(int i=1;i<n;i++){ addEdge(i+1,i,-1,tot); } printf("%d\n",spfa(1)); } }
hdu 3592
#include <stdio.h> #include <algorithm> #include <string.h> #include <iostream> #include <stdlib.h> #include <queue> using namespace std; const int M = 20005; const int N = 1005; const int INF = 99999999; struct Edge { int v,w,next; } edge[M]; int head[N]; int n; bool vis[N]; int time[N],low[N]; int spfa(int s) { queue<int> q; for(int i=1; i<=n; i++) { vis[i] = false; low[i] = INF; time[i] = 0; } low[s] = 0; time[s]++; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for(int k = head[u]; k!=-1; k=edge[k].next) { int v = edge[k].v,w = edge[k].w; if(low[v]>low[u]+w) { low[v] = low[u]+w; if(!vis[v]) { vis[v] = true; q.push(v); if(time[v]++>n) return -1; } } } } if(low[n]==INF) return -2; return low[n]; } void addEdge(int u,int v,int w,int &k) { edge[k].v = v,edge[k].w = w,edge[k].next = head[u],head[u]=k++; } int main() { int ml,md; int tcase; scanf("%d",&tcase); while(tcase--) { scanf("%d%d%d",&n,&ml,&md); memset(head,-1,sizeof(head)); int u,v,w; int tot = 0; for(int i=0; i<ml; i++) { scanf("%d%d%d",&u,&v,&w); addEdge(u,v,w,tot); } for(int i=0; i<md; i++) { scanf("%d%d%d",&u,&v,&w); addEdge(v,u,-w,tot); } for(int i=1; i<n; i++) { addEdge(i+1,i,-1,tot); } printf("%d\n",spfa(1)); } }