bzoj3890【Usaco2015 Jan】Meeting Time

3890: [Usaco2015 Jan]Meeting Time

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 126   Solved: 81
[ Submit][ Status][ Discuss]

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.
给出一个n个点m条边的有向无环图,每条边两个边权。 
n<=100,没有重边。 
然后要求两条长度相同且尽量短的路径, 
路径1采用第一种边权,路径2采用第二种边权。 
没有则输出”IMPOSSIBLE”

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

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.

HINT

Source




动态规划

用f1[i][j]和f2[i][j]分别表示两个人在j的时间能否到达i点,按照顺序更新f数组,最后判断使f1[n][i]和f2[n][i]都为true的最小的i,如果不存在则输出IMPOSSIBLE。




#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define LL long long
#define MAXN 5000
using namespace std;
int cnt=0,n,m,x,y,t1,t2,head[105];
bool f1[105][10005],f2[105][10005];
struct edge_type
{
	int to,next,w1,w2;
}e[MAXN];
int read()
{
	int x=0;
	char ch=getchar();
	while (ch<'0'||ch>'9') ch=getchar();
	while (ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x;
}
void add_edge(int u,int v,int w1,int w2)
{
	e[++cnt].to=v;
	e[cnt].w1=w1;
	e[cnt].w2=w2;
	e[cnt].next=head[u];
	head[u]=cnt;
}
int main()
{
	memset(head,0,sizeof(head));
	n=read(); m=read();
	F(i,1,m)
	{
		x=read();y=read();t1=read();t2=read();
		add_edge(x,y,t1,t2);
	}
	memset(f1,false,sizeof(f1));
	memset(f2,false,sizeof(f2));
	f1[1][0]=f2[1][0]=true;
	F(i,1,n-1)
	{
		for(int p=head[i];p;p=e[p].next)
		{
			F(j,e[p].w1,10001) f1[e[p].to][j]|=f1[i][j-e[p].w1];
			F(j,e[p].w2,10001) f2[e[p].to][j]|=f2[i][j-e[p].w2];
		}
	}
	F(i,1,10001) if (f1[n][i]&&f2[n][i])
	{
		printf("%d\n",i);
		return 0;
	}
	puts("IMPOSSIBLE");
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值