1. 程式人生 > >洛谷 P3116 [USACO15JAN]約會時間Meeting Time

洛谷 P3116 [USACO15JAN]約會時間Meeting Time

題目描述

Bessie and her sister Elsie want to travel from the barn to theirfavorite 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 acollection of N fields (1 <= N <= 100) numbered 1..N, where field 1contains the barn and field N is the favorite field. The farm is built on theside 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 pathis 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 -> 8direction but not the other way, since this would be uphill. Each pair offields is connected by at most one path, so M <= N(N-1)/2. It might takeBessie 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 Elsieonly consume time when traveling on paths between fields -- since they are in ahurry, they always travel through a field in essentially zero time, neverwaiting around anywhere. Please help determine the shortest amount of timeBessie and Elsie must take in order to reach their favorite field at exactlythe same moment.

輸入

The first input line contains N and M, separated by a space. Eachof the following M lines describes a path using four integers A B C D, where Aand B (with A < B) are the fields connected by the path, C is the timerequired for Bessie to follow the path, and D is the time required for Elsie tofollow the path. Both C and D are in the range 1..100.

輸出

A single integer, giving the minimum time required for Bessie andElsie to travel to their favorite field and arrive at the same moment. If thisis impossible, or if there is no way for Bessie or Elsie to reach the favoritefield at all, output the word IMPOSSIBLE on a single line.

樣例輸入

33

1 3 12

1 2 12

2 3 12

樣例輸出

2

提示:Bessie is twice as fast as Elsie on each path, butif Bessie takes the path 1->2->3 and Elsie takes the path 1->3 theywill arrive at the same time.

題解:我們通過用SPFA計算得出兩頭牛所有的到n號點的路徑的時間(ps:而不是最短距離),然後嘛,用兩個桶來標記,若在兩個桶中都為1,我們要找到其中的最小值輸出即可。
#include <cstdio>
#define N 10005
int n,m,x,y,xx,yy;
int cnt,first[N],next[N],v[N],w[N];
int first1[N],next1[N],v1[N],w1[N];
int q[N*500],q1[N*500],head,tail;
bool ans[N],ans1[N];
using namespace std;
inline void spfa(int x)
{
    head=0;
    tail=1;
    q[1]=x;
    while (head<tail)
    {
        head++;
        for (int i=first[q[head]];i;i=next[i])
        {
            tail++;
            q[tail]=v[i];
            q1[tail]=q1[head]+w[i];
            if (v[i]==n) ans[q1[tail]]=1;
        }
    }
}
inline void spfa1(int x)
{
    head=0;
    tail=1;
    q[1]=x;
    while (head<tail)
    {
        head++;
        for (int i=first1[q[head]];i;i=next1[i])
        {
            tail++;
            q[tail]=v1[i];
            q1[tail]=q1[head]+w1[i];
            if (v1[i]==n) ans1[q1[tail]]=1;
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++)
    {
        scanf("%d%d%d%d",&x,&y,&xx,&yy);
        next[++cnt]=first[x];
        first[x]=cnt;
        v[cnt]=y;
        w[cnt]=xx;
        next1[cnt]=first1[x];
        first1[x]=cnt;
        v1[cnt]=y;
        w1[cnt]=yy;
    }
    spfa(1);
    spfa1(1);
    for (int i=1;i<10005;i++)
      if (ans[i]&&ans1[i])
      {
      	printf("%d\n",i);
      	return 0;
      }
    printf("IMPOSSIBLE\n");
    return 0;
}

相關推薦

P3116 [USACO15JAN]約會時間Meeting Time

題目描述Bessie and her sister Elsie want to travel from the barn to theirfavorite field, such that they leave at exactly the same time from th

USACO15JAN P3116 Meeting time

資料太弱???加個剪枝就過了??? 正解是DP。。。回來我會填坑的 PS:wa了無數遍後,我發現自己的dfs寫錯了。。。。。 原因看註釋吧 #include <algorithm>

P3019 [USACO11MAR]會見點Meeting Place

tmp tin and problem tinc blank layout ren art 題目背景 征求翻譯。如果你能提供翻譯或者題意簡述,請直接發討論,感謝你的貢獻。 題目描述 Bessie and Jonell are great fri

——P3119 [USACO15JAN]草鑒定Grass Cownoisseur

lib return 找到 ast 題目 輸入 ostream starting point P3119 [USACO15JAN]草鑒定Grass Cownoisseur 題目描述 In an effort to better manage the grazing pa

P3119 [USACO15JAN]草鑒定Grass Cownoisseur

stack urn edge empty noi code pan log ace 屠龍寶刀點擊就送 Tarjan縮點+拓撲排序 以後縮點後建圖看n範圍用vector ,或者直接用map+vector 結構體裏數據要清空 代碼: #include <

P2920 [USACO08NOV]時間管理Time Management

completed 現在 data- only time its min enc second P2920 [USACO08NOV]時間管理Time Management 題目描述 Ever the maturing businessma

2889 [USACO07NOV]擠奶的時間Milking Time(DP)(樹狀陣列)

題意 奶牛Bessie在0~N時間段產奶。農夫約翰有M個時間段可以擠奶,時間段f,t內Bessie能擠到的牛奶量e。奶牛產奶後需要休息R小時才能繼續下一次產奶,求Bessie最大的擠奶量。 如果在(si,ti)時刻擠奶,那麼休息完的時間是si+r,即下一次可以擠奶的最早時間是(si+r,..

P3952 時間復雜度

不能 時間復雜度 收獲 進行 return pos 是否 這就是 esp 做這道題的最大收獲就是堅持不懈,勇往直前,這道題是一個“碼力題”,不能說無腦,但絕對惡心。總共花了3h+才調出來。果然當時NOIp放棄這道題是明智的 好了,閑話放一邊,我們來搞一搞這道題。 這道題思路

P3119】[USACO15JAN]草鑒定Grass Cownoisseur

color 影響 縮點 col targe vector () 如果 tex 草鑒定Grass Cownoisseur 題目鏈接 約翰有n塊草場,編號1到n,這些草場由若幹條單行道相連。奶牛貝西是美味牧草的鑒賞家,她想到達盡可能多的草場去品嘗牧草。 貝西總是從1號草場出發,

P3952 時間復雜度(模擬)

str turn har 鏈接 strlen 是個 etop round 自己 題意 題目鏈接 Sol 咕了一年的題解。。就是個模擬吧 考場上寫的遞歸也是醉了。。。 感覺一年自己進步了不少啊。。面向數據編程的能力提高了不少 #include<bits/stdc++.h

[P1425]小魚的游泳時間

題目傳送門 題目描述 倫敦奧運會要到了,小魚在拼命練習游泳準備參加游泳比賽,可憐的小魚並不知道魚類是不能參加人類的奧運會的。這一天,小魚給自己的游泳時間做了精確的計時(本題中的計時都按24小時制計算),它發現自己從a時b分一直游泳到當天的c時d分,請你幫小魚計算一下,它這天一共

【模擬】_3952 時間複雜度

題意 給出幾個語句,判斷它們的時間複雜度是否符合,如果是的話輸出‘Yes’,不然輸出’No’,編譯錯誤的話輸出‘ERR’。 思路 比較長的模擬。 用一個check判斷時間複雜度和是否有編譯錯誤,最後比較

P3952 時間復雜度【字符串】【模擬】

正在 size region algo cout ear its 編譯 編寫程序 題目描述 小明正在學習一種新的編程語言 A++,剛學會循環語句的他激動地寫了好多程序並 給出了他自己算出的時間復雜度,可他的編程老師實在不想一個一個檢查小明的程序, 於是你的機會來啦!下面請你

P3952 時間複雜度 題解

作者:岸芷汀蘭 一、題目: 洛谷原題 二、思路: 很顯然是一道大模擬題,資料結構用棧。 但坑點比較多,先來看一下我的慘痛經歷。 主要有以下坑點: 字串的讀入; 時間複雜度是取最大值; 判錯(包括變數的判重,我用了STL中的set)

P1425 小魚的游泳時間--100分】

#include<iostream> #include<math.h> #include<cstdio> using namespace std; int main

動態規劃背包問題 P1064 金明的預算方案

輸出 ret 設計 div 輸入輸出 style 乘號 輸入輸出格式 sin P1064 金明的預算方案 題目描述 金明今天很開心,家裏購置的新房就要領鑰匙了,新房裏有一間金明自己專用的很寬敞的房間。更讓他高興的是,媽媽昨天對他說:“你的房間需要購買哪些物品,怎麽布置,你

P1352 沒有上司的舞會

整數 urn read getc -s blog 計算 情況 def 題目描述 某大學有N個職員,編號為1~N。他們之間有從屬關系,也就是說他們的關系就像一棵以校長為根的樹,父結點就是子結點的直接上司。現在有個周年慶宴會,宴會每邀請來一個職員都會增

——P1351 聯合權值

problem org cto 輸入 最大的 -m http color 說明 https://www.luogu.org/problem/show?pid=1351 題目描述 無向連通圖G 有n 個點,n - 1 條邊。點從1 到n 依次編號,編號為 i 的點的權值為

——P1352 沒有上司的舞會

tps 否則 pre www using 題目 表示 i++ color https://www.luogu.org/problem/show?pid=1352#sub 題目描述 某大學有N個職員,編號為1~N。他們之間有從屬關系,也就是說他們的關系就像一棵以校長為根的

P3398 倉鼠找sugar

網上 etc 最短路徑 pan pac space nbsp -m 不能 題目描述 小倉鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每個節點的編號為1~n。地下洞穴是一個樹形結構。這一天小倉鼠打算從從他的臥室(a)到餐廳(b),而他的基友同時要從他的臥室(c)