洛谷 P3116 [USACO15JAN]约会时间Meeting Time

题目描述

Bessie and her sister Elsie want to travel from the barn to theirfavorite 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 acollection of N fields (1 <= N <= 100) numbered 1..N, where field 1contains the barn and field N is the favorite field. The farm is built on theside of a hill, with field X being higher in elevation than field Y if X 8direction but not the other way, since this would be uphill. Each pair offields is connected by at most one path, so M <= N(N-1)/2. It might takeBessie 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 Elsieonly consume time when traveling on paths between fields -- since they are in ahurry, they always travel through a field in essentially zero time, neverwaiting around anywhere. Please help determine the shortest amount of timeBessie and Elsie must take in order to reach their favorite field at exactlythe same moment.

输入

The first input line contains N and M, separated by a space. Eachof the following M lines describes a path using four integers A B C D, where Aand B (with A < B) are the fields connected by the path, C is the timerequired for Bessie to follow the path, and D is the time required for Elsie tofollow the path. Both C and D are in the range 1..100.

输出

A single integer, giving the minimum time required for Bessie andElsie to travel to their favorite field and arrive at the same moment. If thisis impossible, or if there is no way for Bessie or Elsie to reach the favoritefield at all, output the word IMPOSSIBLE on a single line.

样例输入 

33

1 3 12

1 2 12

2 3 12

样例输出 

2

提示:Bessie is twice as fast as Elsie on each path, butif Bessie takes the path 1->2->3 and Elsie takes the path 1->3 theywill arrive at the same time.

题解:我们通过用SPFA计算得出两头牛所有的到n号点的路径的时间(ps:而不是最短距离),然后嘛,用两个桶来标记,若在两个桶中都为1,我们要找到其中的最小值输出即可。
#include 
#define N 10005
int n,m,x,y,xx,yy;
int cnt,first[N],next[N],v[N],w[N];
int first1[N],next1[N],v1[N],w1[N];
int q[N*500],q1[N*500],head,tail;
bool ans[N],ans1[N];
using namespace std;
inline void spfa(int x)
{
    head=0;
    tail=1;
    q[1]=x;
    while (head


你可能感兴趣的:(SPFA)