洛谷 1938 [USACO09NOV]找工就业Job Hunt
洛谷 1938? [USACO09NOV]找工就業(yè)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.
奶牛們正在找工作。農(nóng)場主約翰知道后,鼓勵(lì)奶牛們四處碰碰運(yùn)氣。而且他還加了一條要求:一頭牛在一個(gè)城市最多只能賺D(1≤D≤1000)美元,然后它必須到另一座城市工作。當(dāng)然,它可以在別處工作一陣子后又回到原來的城市再最多賺D美元。而且這樣的往返次數(shù)沒有限制。
城市間有P(1≤P≤150)條單向路徑連接,共有C(2≤C≤220)座城市,編號(hào)從1到C。奶牛貝茜當(dāng)前處在城市S(1≤S≤C)。路徑i從城市A_i到城市B_i(1≤A_i≤C,1≤B_i≤C),在路徑上行走不用任何花費(fèi)。
為了幫助貝茜,約翰讓它使用他的私人飛機(jī)服務(wù)。這項(xiàng)服務(wù)有F條(1≤F≤350)單向航線,每條航線是從城市J_i飛到另一座城市K_i(1≤J_i≤C,1≤K_i≤C),費(fèi)用是T_i(1≤T_i≤50000)美元。如果貝茜手中沒有現(xiàn)錢,可以用以后賺的錢來付機(jī)票錢。
貝茜可以選擇在任何時(shí)候,在任何城市退休。如果在工作時(shí)間上不做限制,貝茜總共可以賺多少錢呢?如果賺的錢也不會(huì)出現(xiàn)限制,就輸出-1。
輸入輸出格式
輸入格式:
第一行:5個(gè)用空格分開的整數(shù)D,P,C,F,S。
第2到第P+1行:第i+1行包含2個(gè)用空格分開的整數(shù),表示一條從城市A_i到城市B_i的單向路徑。
接下來F行,每行3個(gè)用空格分開的整數(shù),表示一條從城市J_i到城市K_i的單向航線,費(fèi)用是T_i。
輸出格式:
一個(gè)整數(shù),在上述規(guī)則下最多可以賺到的錢數(shù)。
輸入輸出樣例
輸入樣例#1:?100 3 5 2 1 1 5 2 3 1 4 5 2 150 2 5 120 輸出樣例#1:?
250
說明
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
這個(gè)世界上有五個(gè)城市,三條單向路徑和兩條單向航線。貝茜從一號(hào)城市開始她的旅行,她在離開一個(gè)城市前最多只能在這個(gè)城市賺100美元。
貝茜可以通過從一號(hào)城市-->五號(hào)城市-->二號(hào)城市-->三號(hào)城市的旅行賺到4*100-150=250美元。
(注:在四個(gè)城市各賺100美元,從五號(hào)城市飛到二號(hào)城市花掉150美元)
來源:USACO 2009 十一月銀組
題解:
可以把每條邊的花費(fèi)看成負(fù)數(shù),更新每個(gè)點(diǎn)的最長路時(shí)加上這個(gè)點(diǎn)的點(diǎn)權(quán)。如果出現(xiàn)環(huán),那么肯定可以賺無限多的錢。建圖時(shí)將航線的邊權(quán)定為花費(fèi),沒用航線的路線花費(fèi)就是0了。?由于需要判斷是否有環(huán),所以需要用到spfa。找最長路啊。
代碼:
#include<cstdio> #include<queue> const int inf=9999999999; struct Edge {int to,next,v; } e[502]; int head[502],dis[502],time[502],d,p,c,f,s,nxt,ans=-1;; std::queue<int>q; bool vis[502]; int max(int x,int y){return x>y?x:y; } void add(int u,int v,int w) {e[++nxt].to=v;e[nxt].next=head[u];e[nxt].v=w;head[u]=nxt; } void spfa() {for(int i=1; i<=c; i++)dis[i]=-inf;dis[s]=d,q.push(s),vis[s]=1;while(!q.empty()) {int u=q.front();q.pop(),vis[u]=0;for(int i=head[u]; i; i=e[i].next) {int v=e[i].to;if(dis[v]<dis[u]-e[i].v+d) {dis[v]=dis[u]-e[i].v+d;time[v]++;if(time[v]>c)return ;if(!vis[v]) {q.push(v);vis[v]=1;}}}}for(int i=1; i<=c; i++)ans=max(ans,dis[i]); } int main() {scanf("%d%d%d%d%d",&d,&p,&c,&f,&s);for(int x,y,i=1; i<=p; i++) scanf("%d%d",&x,&y),add(x,y,0);for(int i=1,x,y,z; i<=f; i++) scanf("%d%d%d",&x,&y,&z),add(x,y,z);spfa();printf("%d",ans);return 0; } AC?
轉(zhuǎn)載于:https://www.cnblogs.com/GTBA/p/9090748.html
總結(jié)
以上是生活随笔為你收集整理的洛谷 1938 [USACO09NOV]找工就业Job Hunt的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Intel技术教程文档【持续更新】
- 下一篇: JavaScript 引用类型 读书笔记