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 (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: (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.

输出格式:

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.

输入输出样例

输入样例#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.

dp[i][j],i表示点i,j表示权值大小。

bool型,如果为true表示该点可以在该时间到达,反之不行。
遍历从第一个点开始能到达的所有的点,能的话为ture ,遍历dp[n][1……10000]是否可以满足两个dp同时为真,不
能为impossible,因为每条边最多是100,从第一个点走到最后一个点最多有100条边,所以一定是小于10000的

#include<iostream>
#include<cstdio>

const int N=10005;
bool dp[105][N];
bool dp2[105][N];

struct node {
    int v,next,w1,w2;
}edge[N];
int num,head[N];
void add_edge(int u,int v,int w1,int w2)
{
    edge[++num].v=v;edge[num].w1=w1;edge[num].w2=w2;edge[num].next=head[u];head[u]=num;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int u,v,a1,a2;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d%d",&u,&v,&a1,&a2);
        add_edge(u,v,a1,a2);
    }
    dp[1][0]=dp2[1][0]=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=head[i];j;j=edge[j].next)
        {
            int v=edge[j].v;
            for(int k=edge[j].w1;k<=10001;k++)
                dp[v][k]|=dp[i][k-edge[j].w1];
            for(int k=edge[j].w2;k<=10001;k++)
                dp2[v][k]|=dp2[i][k-edge[j].w2];
        }
    }
    for(int i=1;i<=10001;i++)
        if(dp[n][i]&&dp2[n][i])
        {
            printf("%d\n",i);
            return 0;
        }
    printf("IMPOSSIBLE\n");
    return 0;
}
时间: 2024-10-09 07:46:46

luogu P3116 [USACO15JAN]会议时间Meeting Time的相关文章

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

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

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

【Luogu】P3116会议时间(拓扑排序,DP)

题目链接 本题使用拓扑排序来规划DP顺序.设s[i][j]表示i步是否能走到j这个点,e[i][j]表示i步是否能走到j这个点——用第二条路径.因为要满足无后效性和正确性,只有第i个点已经全部更新完毕的时候才能用它来更新其他的点.所以用拓扑. 代码如下 #include<cstdio> #include<cctype> #include<cstring> #include<algorithm> using namespace std; inline long

2017会议时间

sigkdd deadline:February 17, 2017 position:Halifax, Nova Scotia - Canada conference time:August 13 - 17, 2017 ICML: deadline:Feb 24, 2017 position:Sydney AUSTRALIA conference time:Aug 6, 2017 - Aug 11, 2017 IJCAI: Abstract submission: February 16th,

luogu 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 cow path connecting a pair of fields. For

Luogu 3119 [USACO15JAN]草鉴定Grass Cownoisseur

思路很乱,写个博客理一理. 缩点 + dp. 首先发现把一个环上的边反向是意义不大的,这样子不但不好算,而且相当于浪费了一次反向的机会.反正一个强连通分量里的点绕一遍都可以走到,所以我们缩点之后把一个强连通分量放在一起处理. 设$st$表示缩点之后$1$所在的点,设$f_{x}$表示从$st$走到$x$的最长链,$g_{x}$表示从$x$走到$st$的最长链,因为把一个$DAG$上的边反向一下并不会走重复的点,那么我们最后枚举一下边$(x, y)$,把它反向,这样子$f_{x} + g_{y}

First scrum meeting report - 151017

提要 今天开会主要是讨论一下北航MOOC客户端的具体要求和每个人的大致分工.会议后来还简单商讨了一下我们app的大致界面框架. 会议地点:大运村KFC 会议时间:2015年10月17日,15:00-17:30 任务和分工 我们将开发工作大致分为如下几块: 课程详情(含视频播放,下载,讨论区) 用户部分(含用户注册,用户信息查看与修改,设置,我的选课) 总体框架(用户登陆,主界面,各个模块的集成工作) 服务器(工作量待商讨,因为我们暂时不能得知学校服务器的数据情况) 此外,还有UI设计与后期测试工

LeetCode 252. Meeting Rooms (会议室)

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. For example,Given [[0, 30],[5, 10],[15, 20]],return false. 题目标签:sort 这道题目给了我们一个array的会议时间,让我们