首页 > 代码库 > P3116 [USACO15JAN]会议时间Meeting Time

P3116 [USACO15JAN]会议时间Meeting Time

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.

 

输入输出样例

输入样例#1:
3 3 1 3 1 2 1 2 1 2 2 3 1 2 
输出样例#1:
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.

 

分析

这道题可以用动规做,f[i][j]表示经过长度为j的路径能否到达i点,g[][]数组同理,只不过是题目中的第二组数据。

然后我们会用到拓扑,为什么用拓扑呢?因为要满足动规的无后效性,必须要用已经完成(没有点可以在更改当他的值,即没有边在通向他)的点,这一点很重要!!!

但是一件惊人的件事情:不用拓扑居然也可能过!!!你在逗我吗?提交代码时写错了个地方,代码第28行的ru[b]++忘记写了,就交了,然后呢,就A了,,,我能说啥。。。

没加28行,意味着说有的点会按顺序加入队列中,然后相当于枚举了所有的点一遍而已,也就没用到拓扑。数据也没这么弱的吧!!!

注意:46行不能有等号,数组最大是MAXE-1,不然会RE!

代码

 1 #include<cstdio> 2 #include<algorithm> 3 #include<queue> 4 #include<cstring> 5 using namespace std; 6  7 const int MAXN = 110; 8 const int MAXE = 10010; 9 struct Edge{10     int to,nxt,w1,w2;11 }e[MAXE];12 bool f[MAXN][MAXE],g[MAXN][MAXE],vis[MAXN];13 int head[MAXN],ru[MAXN];14 int n,m,tot;15 queue<int>q;16 17 int main()18 {19     scanf("%d%d",&n,&m);20     for (int x,y,a,b,i=1; i<=m; ++i)21     {22         scanf("%d%d%d%d",&a,&b,&x,&y);23         e[++tot].nxt = head[a];    //建边 24         e[tot].to = b;25         e[tot].w1 = x;26         e[tot].w2 = y;27         head[a] = tot;28         ++ru[b];29     }30     for (int i=1; i<=n; ++i)    //拓扑 31         if (!ru[i]) q.push(i);32     f[1][0] = g[1][0] = true;33     while (!q.empty())34     {35         int u = q.front();36         q.pop();37         for (int i=head[u]; i; i=e[i].nxt)38         {39             int w1 = e[i].w1, w2 = e[i].w2, v = e[i].to;40             for (int j=0; j+w1<MAXE; ++j) f[v][j+w1] |= f[u][j];41             for (int j=0; j+w2<MAXE; ++j) g[v][j+w2] |= g[u][j];42             ru[v]--;43             if (!ru[v]) q.push(v);44         }45     }46     for (int i=0; i<MAXE; ++i)47         if (f[n][i]&&g[n][i]){ printf("%d",i); return 0; }48     printf("IMPOSSIBLE");49     return 0;50 }

 

P3116 [USACO15JAN]会议时间Meeting Time