首页 > 代码库 > luogu P3116 [USACO15JAN]会议时间Meeting Time
luogu P3116 [USACO15JAN]会议时间Meeting Time
题目描述
Bessie and her sister Elsie want to travel from the barn to their favorite field, such that they leave at exactly the same time from the barn, and also arrive at exactly the same time at their favorite field.
The farm is a collection of N fields (1 <= N <= 100) numbered 1..N, where field 1 contains the barn and field N is the favorite field. The farm is built on the side of a hill, with field X being higher in elevation than field Y if X < Y. An assortment of M paths connect pairs of fields. However, since each path is rather steep, it can only be followed in a downhill direction. For example, a path
connecting field 5 with field 8 could be followed in the 5 -> 8 direction but not the other way, since this would be uphill. Each pair of fields is connected by at most one path, so M <= N(N-1)/2.
It might take Bessie and Elsie different amounts of time to follow a path; for example, Bessie might take 10 units of time, and Elsie 20. Moreover, Bessie and Elsie only consume time when traveling on paths between fields -- since they are in a hurry, they always travel through a field in essentially zero time, never waiting around anywhere.
Please help determine the shortest amount of time Bessie and Elsie must take in order to reach their favorite field at exactly the same moment.
给出一个n个点m条边的有向无环图,每条边两个边权。
n<=100,没有重边。
然后要求两条长度相同且尽量短的路径,
路径1采用第一种边权,路径2采用第二种边权。
没有则输出”IMPOSSIBLE”
输入输出格式
输入格式:
INPUT: (file meeting.in)
The first input line contains N and M, separated by a space.
Each of the following M lines describes a path using four integers A B C D, where A and B (with A < B) are the fields connected by the path, C is the time required for Bessie to follow the path, and D is the time required for Elsie to follow the path. Both C and D are in the range 1..100.
输出格式:
OUTPUT (file meeting.out)
A single integer, giving the minimum time required for Bessie and
Elsie to travel to their favorite field and arrive at the same moment.
If this is impossible, or if there is no way for Bessie or Elsie to reach
the favorite field at all, output the word IMPOSSIBLE on a single line.
输入输出样例
3 3 1 3 1 2 1 2 1 2 2 3 1 2
2
说明
SOLUTION NOTES:
Bessie is twice as fast as Elsie on each path, but if Bessie takes the
path 1->2->3 and Elsie takes the path 1->3 they will arrive at the
same time.
bool型,如果为true表示该点可以在该时间到达,反之不行。
遍历从第一个点开始能到达的所有的点,能的话为ture ,遍历dp[n][1……10000]是否可以满足两个dp同时为真,不
能为impossible,因为每条边最多是100,从第一个点走到最后一个点最多有100条边,所以一定是小于10000的
#include<iostream>#include<cstdio>const int N=10005;bool dp[105][N];bool dp2[105][N];struct node { int v,next,w1,w2;}edge[N];int num,head[N];void add_edge(int u,int v,int w1,int w2) { edge[++num].v=v;edge[num].w1=w1;edge[num].w2=w2;edge[num].next=head[u];head[u]=num;}int main() { int n,m; scanf("%d%d",&n,&m); int u,v,a1,a2; for(int i=1;i<=m;i++) { scanf("%d%d%d%d",&u,&v,&a1,&a2); add_edge(u,v,a1,a2); } dp[1][0]=dp2[1][0]=1; for(int i=1;i<=n;i++) { for(int j=head[i];j;j=edge[j].next) { int v=edge[j].v; for(int k=edge[j].w1;k<=10001;k++) dp[v][k]|=dp[i][k-edge[j].w1]; for(int k=edge[j].w2;k<=10001;k++) dp2[v][k]|=dp2[i][k-edge[j].w2]; } } for(int i=1;i<=10001;i++) if(dp[n][i]&&dp2[n][i]) { printf("%d\n",i); return 0; } printf("IMPOSSIBLE\n"); return 0;}
luogu P3116 [USACO15JAN]会议时间Meeting Time