洛谷P3116 [USACO15JAN]约会时间Meeting Time【DP】

时空限制 1000ms / 128MB

题目描述

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同时出发并且同时到达她们喜欢的田地的最短的时间。

输入格式:

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"。


题目分析

d p [ 0 / 1 ] [ i ] [ j ] dp[0/1][i][j] dp[0/1][i][j]表示Bessie 0/Elsie 1是否存在一条从 1 1 1 i i i的路径长度为 j j j
直接BFS递推更新即可
注意如果如果当前继续扩展时发现已经有 d p [ 0 / 1 ] [ i ] [ j ] = = 1 dp[0/1][i][j]==1 dp[0/1][i][j]==1,则这个状态不在继续加入队列
对于到达终点的状态也不再加入队列


#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long lt;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int maxn=110;
int n,m;
struct edge{int v,dis[2],nxt;}E[maxn*maxn];
int head[maxn],tot;
struct node{int u,dis,d;};
int dp[2][maxn][maxn*maxn];
int ans=1e9;

void add(int u,int v,int dis0,int dis1)
{
    E[++tot].nxt=head[u];
    E[tot].dis[0]=dis0; E[tot].dis[1]=dis1;
    E[tot].v=v; 
    head[u]=tot;
}

void bfs()
{
    queue<node> q; 
    dp[0][1][0]=dp[1][1][0]=1;
    q.push((node){1,0,0}); q.push((node){1,0,1});
    while(!q.empty())
    {
        node cur=q.front(); q.pop();
        int u=cur.u,dis=cur.dis,d=cur.d;
        for(int i=head[u];i;i=E[i].nxt)
        {
            int v=E[i].v,nxtd=dis+E[i].dis[d];
            if(!dp[d][v][nxtd])
            {
                dp[d][v][nxtd]=1;
                if(v!=n)q.push((node){v,nxtd,d});
                if(v==n&&dp[d^1][v][nxtd]&&nxtd<ans) ans=nxtd;
            }
        }
    }
}

int main()
{
    n=read();m=read();
    for(int i=1;i<=m;++i)
    {
    	int u=read(),v=read();
        int dis0=read(),dis1=read();
    	if(u>v) swap(u,v);
    	add(u,v,dis0,dis1);
    }
    bfs();
    
    if(ans!=1e9) printf("%d",ans);
    else printf("IMPOSSIBLE");
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值