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.

Bessie和她的妹妹Elsie想从粮仓去她们最喜欢的田地,也就是能够使她们一起从粮仓离开,并且能同一时间到达的田地。

这个农场是由N块(1 <= N <= 100)编号为1..N的田地构成的,第一块田地就是粮仓,并且第N块田地是她们最喜欢的田地。这个农场建在山的一边,所以,如果 X < Y 的话则满足第X块田地的高度要高于第Y块田地的高度。在这之中,有M条交错纵横的路径将不同的田地连接起来。不过,显而易见的是,因为每条路都太陡了,所以这些小路只能沿着从高到低的方向走。例如,一条连接第5块田地和第8块田地的小路只能沿着 5 -> 8 的方向走,而不能沿着其他方向,因为那样会成为上坡路。每两块田地最多只能有一条路径相连接,所以一定有 M <= N(N-1)/2。

有可能的是,Bessie和Elsie两个人走同一条小路会耗费不同的时间;比如,通过同一条小路,Bessie可能会耗费10个单位的时间,而Elsie会耗费20个单位的时间。此外,Bessie和Elsie只会在通过连接两块田地的小路时耗费时间——因为她们太匆忙了,在穿过田地时不会耗费任何时间,也从来不在任何地方停下来等待。

现在,请你判断出,能够满足使Bessie和Elsie同时出发并且同时到达她们喜欢的田地的最短的时间。

输入输出格式

输入格式:

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.

第一行输入N和M,中间用空格分开。

接下来的M行,每行有四个整型A B C D,其中,A和B(A < B)代表着两块用这条小路连接的田地,C代表Bessie通过这条小路的时间,而D代表Elsie通过这条小路的时间。C和D均在 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.

一个整型,输出的是能够使两人同时出发并且同时到达目的地的最短时间,如果没有满足条件的答案,则输出"IMPOSSIBLE"。

输入输出样例

输入样例#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]表示走i的路程能否到达第j个点,g[i][j]是第二个人的同理,拓扑排序+暴力dp即可
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 110
using namespace std;
int n,m,num,head[maxn],du[maxn];
bool f[][],g[][];
struct node{int to,pre,v1,v2;}e[];
void Insert(int from,int to,int v1,int v2){
e[++num].to=to;
e[num].v1=v1;
e[num].v2=v2;
e[num].pre=head[from];
head[from]=num;
}
queue<int>q;
int main(){
scanf("%d%d",&n,&m);
int x,y,w1,w2;
for(int i=;i<=m;i++){
scanf("%d%d%d%d",&x,&y,&w1,&w2);
if(x>y)swap(x,y);
Insert(x,y,w1,w2);
du[y]++;
}
for(int i=;i<=n;i++)if(!du[i])q.push(i);
f[][]=g[][]=;
while(!q.empty()){
int now=q.front();q.pop();
for(int i=head[now];i;i=e[i].pre){
int to=e[i].to;
int vv=min(e[i].v1,e[i].v2);
for(int j=vv;j<=;j++){
if(j>=e[i].v1)f[j][to]|=f[j-e[i].v1][now];
if(j>=e[i].v2)g[j][to]|=g[j-e[i].v2][now];
}
du[to]--;
if(du[to]==)q.push(to);
}
}
for(int i=;i<=;i++)
if(f[i][n]&&g[i][n]){
printf("%d",i);
return ;
}
puts("IMPOSSIBLE");
return ;
}

洛谷P3116 [USACO15JAN]约会时间Meeting Time的更多相关文章

  1. P3116 [USACO15JAN]会议时间Meeting Time

    P3116 [USACO15JAN]会议时间Meeting Time 题目描述 Bessie and her sister Elsie want to travel from the barn to ...

  2. luogu P3116 [USACO15JAN]会议时间Meeting Time

    题目描述 Bessie and her sister Elsie want to travel from the barn to their favorite field, such that the ...

  3. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur 解题报告

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 约翰有\(n\)块草场,编号1到\(n\),这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可 ...

  4. 洛谷 P3019 [USACO11MAR]会见点Meeting Place

    题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 Bessie and Jonell are great friends. Since Farmer John ...

  5. 洛谷——P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of hi ...

  6. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur (SCC缩点,SPFA最长路,枚举反边)

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of hi ...

  7. 洛谷3119 [USACO15JAN]草鉴定Grass Cownoisseur

    原题链接 显然一个强连通分量里所有草场都可以走到,所以先用\(tarjan\)找强连通并缩点. 对于缩点后的\(DAG\),先复制一张新图出来,然后对于原图中的每条边的终点向新图中该边对应的那条边的起 ...

  8. 洛谷P3119 USACO15JAN 草鉴定

    题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...

  9. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    屠龙宝刀点击就送 Tarjan缩点+拓扑排序 以后缩点后建图看n范围用vector ,或者直接用map+vector 结构体里数据要清空 代码: #include <cstring> #i ...

随机推荐

  1. spring事务隔离级别以及脏读 不可重复读 幻影读

    隔离级别 声明式事务的第二个方面是隔离级别.隔离级别定义一个事务可能受其他并发事务活动活动影响的程度.另一种考虑一个事务的隔离级别的方式,是把它想象为那个事务对于事物处理数据的自私程度. 在一个典型的 ...

  2. ONVIF测试方法及工具

    设备是否支持ONVIF验证1 ONVIF Test Tool安装1.1PC安装环境要求:装有Microsoft .Net Framework 3.5或以上版本.1.2安装源文件请见:ONVIF Con ...

  3. 将tomcat7解压版注册为windows系统服务

    一.修改service.bat文件(...tomcat7\bin\service.bat) 该文件中共修改两处即可 ①:在文件的开头加入以下设置,分别是java的安装路径.Tomcat的安装路径及服务 ...

  4. POJ2774Long Long Message (后缀数组&后缀自动机)

    问题: The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to ...

  5. [CERC 2008] Suffix reconstruction

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4319 [算法] 首先 , 我们可以求出这个字符串的rank数组 按照SA逐位枚举 , ...

  6. 用Word编辑cnblogs的博文并发布

    听说可以用Word直接编辑文件发布博客,请教加研究,终于配置成功,先分享如下: 1. 在www.cnblogs.com上注册用户. 2. 打开Word,选择博客文章 3. 点击管理账户,新建或更改,按 ...

  7. Erlang generic standard behaviours -- gen_server noblock call

    在Erlang 系统中,经常需要gen_server 进程来处理共享性的数据,也就是总希望一个gen_server 进程来为多个普通进程提供某种通用性的服务,这也是gen_server 设计的初衷.但 ...

  8. netty中的引导Bootstrap服务端

    引导一个应用程序是指对它进行配置,并使它运行起来的过程. 一.Bootstrap 类 引导类的层次结构包括一个抽象的父类和两个具体的引导子类,如图 8-1 所示 服务器致力于使用一个父 Channel ...

  9. WPF实现右键菜单

    ContextMenu类就是用来做右键菜单的对象,对于任何的控件都可以进行对ContextMenu属性的操作进行设置右键菜单的功能. 下面代码就是对一个按钮添加一个WPF右键菜单的功能: < B ...

  10. nmap 快速扫描所有端口

    nmap -sT -sV -Pn -v xxx.xxx.xxx.xxx nmap -sS -p 1-65535 -v 192.168.1.254参数:-sS    TCP SYN扫描    nmap ...