P1938 [USACO09NOV] to find a job Employment Job Hunt

Title Description

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.

The cows are looking for work. After Farmer John knows that encourage cows four chance. And he also added a requirement: a cow in a city can only make up to D (1≤D≤1000) dollars, then it must go to another city to work. Of course, it can return to the original work elsewhere in the city for a while and then re-earn up to $ D. And such number of round trips is not limited.

Between cities P (1≤P≤150) unidirectional path is connected, a total of C (2≤C≤220) cities, numbered from 1 to C. Cows Bessie currently in the city of S (1≤S≤C). Path i (1≤A_i≤C, 1≤B_i≤C), traveling from city to city B_i A_i on a path without any cost.

To help Bessie, let John use his private jet service. The service there (1≤F≤350) one-way route F strips, each route is from the city J_i fly to another city K_i (1≤J_i≤C, 1≤K_i≤C), the cost is T_i (1≤ T_i≤50000) dollars. If Bessie did not have cash, you can later use to earn money to pay for tickets.

Bessie can choose at any time to retire in any city. If the work is not limited in time, Bessie total of how much you can do? If the money does not limit appeared, output -1.

Input Format

First line: 5 with a space-separated integers D, P, C, F, S.

The second line to the first P + 1: i + 1 th line contains two space-separated integers represents a unidirectional path from city to city A_i of B_i.

Next F lines of three space-separated integers to represent a one-way route from city to city K_i J_i, the cost is T_i.

Output Format

An integer number of money under these rules can make the most of.

Sample input and output

Input # 1
100 3 5 2 1
1 5
2 3
1 4
5 2 150
2 5 120
Output # 1
250

Description / Tips

This world has five cities, three paths and two jet routes. Bessie starts out in city 1, and she can only make 100 dollars in each city before moving on.

Bessie can travel from city 1 to city 5 to city 2 to city 3, and make a total of 4*100 - 150 = 250 dollars.

Source: USACO 2009 November Silver

There are five cities, three-way and two-way path routes in the world. Bessie started her trip from One city, she can only earn a maximum of $ 100 in the city before leaving the city.

Bessie can be from 01 city - Urban II> - -> V Cities> Travel III cities earn 4 * 100-150 = $ 250.

(Note: in four cities each make $ 100, the fifth city to fly from city to spend $ 1.5 II)

Source: USACO 2009 Nov silver

 

Why do I always feel that this question did there?

 

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
int cnt=0; 
int d,p,c,f,s;
struct node
{
    int to;
    int next;
    int w;
};
int head[1010];
node edge[1010];
bool book[1010];
int cntt[1010];
int dis[1010];
struct cmp
{
    bool operator()(int a,int b)
    {
        return dis[a]<dis[b];
    }
};

inline int read(){
    int s=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){
            w=-1;
        }
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        s=s*10+ch-'0';
        ch=getchar();
    }
    return s*w;
}


void add(int u,int v,int ww)
{
    edge[++cnt].to=v;
    edge[cnt].w=ww;
    edge[cnt].next=head[u];
    head[u]=cnt;
}

priority_queue<int,vector<int>,cmp>q;

int main(){
    d=read();
    p=read();
    c=read();
    f=read();
    s=read();
    for(int i=1;i<=p;i++){
        int u,v;
        u=read();
        v=read();
        add(u,v,d);
    }
    for(int i=1;i<=f;i++){
        int u,v,w;
        u=read();
        v=read();
        w=read();
        add(u,v,d-w);
    }
    q.push(s);
    book[s]=1;
    dis[s]=d;
    cntt[s]++;
    while(q.empty()==0){
        int u=q.top();
        q.pop();
        book[u]=0;
        cntt[u]++;
        if(cntt[u]>c){
            printf("-1"); 
            return 0;
        }
        for(int i=head[u];i>0;i=edge[i].next){
            if(dis[edge[i].to]<dis[u]+edge[i].w){
                dis[edge[i].to]=dis[u]+edge[i].w;
                if(book[edge[i].to]==0){
                    q.push(edge[i].to);
                    book[edge[i].to]=1;
                }
            }
        }
    }
    int maxx=0;
    for(int i=1;i<=c;i++){
        if(dis[i]>maxx){
            maxx=dis[i];
        }
    }
    printf("%d\n",maxx);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/hrj1/p/11681986.html