[USACO09NOV]Job Hunt

題目描述

Bessie is running out of money and is searching for jobs. Farmer John knows this and wants the cows to travel around so he has imposed a rule that his cows can only make D (1 <= D <= 1,000) dollars in a city before they must work in another city. Bessie can, however, return to a city after working elsewhere for a while and again earn the D dollars maximum in that city. There is no limit on the number of times Bessie can do this.
Bessie’s world comprises P (1 <= P <= 150) one-way paths connecting C (2 <= C <= 220) cities conveniently numbered 1..C. Bessie is currently in city S (1 <= S <= C). Path i runs one-way from city A_i to city B_i (1 <= A_i <= C; 1 <= B_i <= C) and costs nothing to traverse.
To help Bessie, Farmer John will give her access to his private jet service. This service features F (1 <= F <= 350) routes, each of which is a one way flight from one city J_i to a another K_i (1 <= J_i <= C; 1 <= K_i <= C) and which costs T_i (1 <= T_i <= 50,000) dollars. Bessie can pay for the tickets from future earnings if she doesn’t have the cash on hand.
Bessie can opt to retire whenever and wherever she wants. Given an unlimited amount of time, what is the most money that Bessie can make presuming she can make the full D dollars in each city she can travel to? Print -1 if there is no limit to this amount.
奶牛們正在找工作。農場主約翰知道後,鼓勵奶牛們四處碰碰運氣。而且他還加了一條要求:一頭牛在一個城市最多隻能賺D(1≤D≤1000)美元,然後它必須到另一座城市工作。當然,它可以在別處工作一陣子後又回到原來的城市再最多賺D美元。而且這樣的往返次數沒有限制。
城市間有P(1≤P≤150)條單向路徑連接,共有C(2≤C≤220)座城市,編號從1到C。奶牛貝茜當前處在城市S(1≤S≤C)。路徑i從城市A_i到城市B_i(1≤A_i≤C,1≤B_i≤C),在路徑上行走不用任何花費。
爲了幫助貝茜,約翰讓它使用他的私人飛機服務。這項服務有F條(1≤F≤350)單向航線,每條航線是從城市J_i飛到另一座城市K_i(1≤J_i≤C,1≤K_i≤C),費用是T_i(1≤T_i≤50000)美元。如果貝茜手中沒有現錢,可以用以後賺的錢來付機票錢。
貝茜可以選擇在任何時候,在任何城市退休。如果在工作時間上不做限制,貝茜總共可以賺多少錢呢?如果賺的錢也不會出現限制,就輸出-1。

輸入輸出格式

輸入格式:

第一行:5個用空格分開的整數D,P,C,F,S。
第2到第P+1行:第i+1行包含2個用空格分開的整數,表示一條從城市A_i到城市B_i的單向路徑。
接下來F行,每行3個用空格分開的整數,表示一條從城市J_i到城市K_i的單向航線,費用是T_i。

輸出格式:

一個整數,在上述規則下最多可以賺到的錢數。

輸入輸出樣例

輸入樣例

100 3 5 2 1
1 5
2 3
1 4
5 2 150
2 5 120

輸出樣例

250

一個比較簡單的考驗建圖能力的題, 建完圖跑下最長路再判下正環就好。
代碼如下

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int size = 200010;
int head[size],next[size],dist[size];
int tot = 1;
struct gtnd
{
    int t,d;
}l[size];
void build(int f,int t,int d)
{
    l[tot].t = t;
    l[tot].d = d;
    next[tot] = head[f];
    head[f] = tot ++;
}
int d,p,c,m,s;
queue < int > q;
bool use[size];
int tim[size];
int spfa(int s)
{
    for(int i = 1 ; i <= c ; i ++)
        dist[i] = -2147483641;
    dist[s] = 0;
    use[s] = 1;
    q.push(s);
    while(!q.empty())
    {
        int f = q.front();
        q.pop();
        use[f] = 0;
        for(int i = head[f] ; i ; i = next[i])
        {
            int t = l[i].t;
            if(dist[t] < dist[f] + l[i].d)
            {
                dist[t] = dist[f] + l[i].d;
                tim[t] = tim[f] + 1;
                if(tim[t] > c+100)
                    return -1;
                if(!use[t])
                {
                    use[t] = 1;
                    q.push(t);
                }
            }
        }
    }
    int ans = 0;
    for(int i = 1 ; i <= c ; i ++)
        ans = max(ans,dist[i]);
    return ans+d;
}
int main()
{
    scanf("%d%d%d%d%d",&d,&p,&c,&m,&s);
    for(int i = 1 ; i <= p ; i ++)
    {
        int f,t;
        scanf("%d%d",&f,&t);
        build(f,t,d);
    }
    for(int i = 1 ; i <= m ; i ++)
    {
        int f,t,ti;
        scanf("%d%d%d",&f,&t,&ti);
        build(f,t,d-ti);
    }
    int ans = spfa(s);
    printf("%d\n",ans);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章