USACO 2015 Jan Silver 3.Meeting Time

题目描述

Description

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.

给出一个拓扑图,每条有向边有两个权值,有两个人从1出发到n,分别走这两种权值。问有没有权值使得这两个人都能走过这些权值到达n。

Input

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

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.

Sample Input

3 3
1 3 1 2
1 2 1 2
2 3 1 2

Sample Output

2

HINT

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.

题目分析:

拓扑排序+DP 两个bool数组代表分别用边权1,2 是否能从1到to[i] 并花费j

f[to[i]][j]|=f[nmp][j-w1[i]];  g[to[i]][j]|=g[nmp][j-w2[i]]; f[1][0]=g[1][0]=1;

#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
int n,m;
int head[1000],to[100000],next[100000],w1[100000],w2[100000];
int in[1000];
int tot;
void add(int x,int y,int c1,int c2)
{
    next[++tot]=head[x];
    to[tot]=y;
    head[x]=tot;
    w1[tot]=c1;
    w2[tot]=c2;
}
queue<int>q;
int f[104][10010];
int g[104][10010];
void topusort()
{
    for(int i=1;i<=n;i++)
        if(!in[i]) q.push(i);
    while(q.size())
    {
        int nmp=q.front();
        q.pop();
        for(int i=head[nmp];i;i=next[i])
        {
            if(!(--in[to[i]])) q.push(to[i]);
            for(int j=w1[i];j<=10000;j++)
                f[to[i]][j]|=f[nmp][j-w1[i]]; 
            for(int j=w2[i];j<=10000;j++)
                g[to[i]][j]|=g[nmp][j-w2[i]];
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    f[1][0]=g[1][0]=1;
    for(int i=1;i<=m;i++)
    {
        int x,y,c1,c2;
        scanf("%d%d%d%d",&x,&y,&c1,&c2);
        add(x,y,c1,c2);
        in[y]++;
    }
    topusort();
    for(int i=0;i<=10000;i++)
    {
        if(f[n][i]&&g[n][i])
        {
            printf("%d",i);
            return 0;
        }
    }
    printf("IMPOSSIBLE");
}

发表评论

邮箱地址不会被公开。 必填项已用*标注