洛谷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.

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[500010][110],g[500010][110];
struct node{int to,pre,v1,v2;}e[500010];
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=1;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=1;i<=n;i++)if(!du[i])q.push(i);
    f[0][1]=g[0][1]=1;
    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<=10010;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]==0)q.push(to);
        }
    }
    for(int i=0;i<=10010;i++)
        if(f[i][n]&&g[i][n]){
            printf("%d",i);
            return 0;
        }
    puts("IMPOSSIBLE");
    return 0;
}

原文地址:https://www.cnblogs.com/thmyl/p/8254098.html

时间: 2024-12-22 04:43:21

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

P3116 [USACO15JAN]约会时间Meeting Time

自带拓扑图结构,对可行性求交集即可 #include<bits/stdc++.h> using namespace std; int f[101][11000],g[101][11000],n,m,a,b,c,d,head[1001],ne; struct node{int nxt,to,w1,w2;}eg[100100]; void adde(int from,int to,int vl1,int vl2) {eg[++ne].nxt=head[from];eg[ne].to=to;eg[n

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. T

luogu 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

洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur

P3119 [USACO15JAN]草鉴定Grass Cownoisseur tarjan缩点,正反spfa,枚举边,更新最大值 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define maxn 1000000 4 #define inf 0x3f3f3f3f 5 int n,m,x[maxn],y[maxn],z,num,head[maxn],head2[maxn],tim,ans,tot,dis1[maxn],dis2[maxn

洛谷-小鱼的游泳时间-洛谷的第一个任务

题目描述 Description 伦敦奥运会要到了,小鱼在拼命练习游泳准备参加游泳比赛,可怜的小鱼并不知道鱼类是不能参加人类的奥运会的.这一天,小鱼给自己的游泳时间做了精确的计时(本题中的计时都按24小时制计算),它发现自己从a时b分一直游泳到当天的c时d分,请你帮小鱼计算一下,它这天一共游了多少时间呢?小鱼游的好辛苦呀,你可不要算错了哦. 输入输出格式 Input/output 输入格式:一行内输入4个整数,分别表示a,b,c,d.输出格式:一行内输出2个整数e和f,用空格间隔,依次表示小鱼这

洛谷 P3019 [USACO11MAR]会见点Meeting Place

题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 Bessie and Jonell are great friends. Since Farmer John scrambles where the cows graze every day, they are sometimes quite far from each other and can't talk. The pastures and paths on FJ's farm form a 'tre

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

P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-way cow paths all over his farm. The farm consists of N fields, conveniently numbered 1..N, with each one-way co

洛谷 P1193 洛谷团队训练VS传统团队训练

P1193 洛谷团队训练VS传统团队训练 题目背景 “在中学的信息学教育领域,洛谷无疑是一个相当受欢迎的辅助网站.同时有百余所学校正在通过洛谷进行信息学竞赛(以后简称OI)的教育.洛谷之所以如此受欢迎,是因为洛谷创新的将OI教育的几乎每一个环节都搬到了线上,无论是学校的竞赛教练还是学生,均可以仅仅使用这一个网站来进行练习,提升自己的能力.” ——摘自<厦门中小学教育科学研究>,2015年2月号. 题目描述 XX中学的两位信息组的教练正在为学校信息组是否应当将洛谷作为主要的训练工具而争论不休,最

洛谷 P2827 BZOJ 4721 UOJ #264 蚯蚓

题目描述 本题中,我们将用符号表示对c向下取整,例如:. 蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭蚯蚓. 蛐蛐国里现在共有n只蚯蚓(n为正整数).每只蚯蚓拥有长度,我们设第i只蚯蚓的长度为,并保证所有的长度都是非负整数(即:可能存在长度为0的蚯蚓). 每一秒,神刀手会在所有的蚯蚓中,准确地找到最长的那一只(如有多个则任选一个)将其切成两半.神刀手切开蚯蚓的位置由常数p(是满足0<p<1的有理数)决定,设这只蚯蚓长度为x,神刀手会将其切成两只长度