日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

P1938 [USACO09NOV]Job Hunt S(spfa,队列+邻接表优化)

發布時間:2023/12/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 P1938 [USACO09NOV]Job Hunt S(spfa,队列+邻接表优化) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目描述

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。

輸出格式

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

輸入輸出樣例

輸入 #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

這個世界上有五個城市,三條單向路徑和兩條單向航線。貝茜從一號城市開始她的旅行,她在離開一個城市前最多只能在這個城市賺100美元。

貝茜可以通過從一號城市-->五號城市-->二號城市-->三號城市的旅行賺到4*100-150=250美元。

(注:在四個城市各賺100美元,從五號城市飛到二號城市花掉150美元)

來源:USACO 2009 十一月銀組

分析:

把d轉化為 邊權值,即求最長路徑,再取負得最短路徑 ,spfa處理;

有負權環說明沒有最短路徑,輸出-1;

#include <bits/stdc++.h> using namespace std; const int inf=0x3f3f3f3f; int d; int m;int n; int f; int s; int dis[290]; int vis[290]; int cnt[290]; vector<pair<int,int>>v[290]; int spfa(){memset(dis,0x3f,sizeof(dis));dis[s]=0;queue<int>q; //隊列,int型,存放節點編號q.push(s);vis[s]=1;int i;while(!q.empty()){ //1 5 -100int cur=q.front(); //2 3 -100q.pop(); //1 4 -100vis[cur]=0; //5 2 50cnt[cur]++; //2 5 20if(cnt[cur]>=n){//一個點進隊到達n次說明有負權環,也就沒有最短路徑cout<<-1;return 0;}for(i=0;i<(int)v[cur].size();i++){if(dis[v[cur][i].first]>dis[cur]+v[cur][i].second){dis[v[cur][i].first]=dis[cur]+v[cur][i].second;if(vis[v[cur][i].first]==0){vis[v[cur][i].first]=1;q.push(v[cur][i].first);}}}}return 1; } int main (){ios::sync_with_stdio(false);cin.tie(0);cin>>d>>m>>n>>f>>s;int i,j;for(i=1;i<=m;i++){int x,y;cin>>x>>y;v[x].emplace_back(y,-d);}for(i=1;i<=f;i++){int x,y,w;cin>>x>>y>>w;v[x].emplace_back(y,-d+w);}int res=inf;if(spfa()){for(i=1;i<=n;i++){res=min(res,dis[i]);}res=-res;res+=d;cout<<res;}return 0; }

總結

以上是生活随笔為你收集整理的P1938 [USACO09NOV]Job Hunt S(spfa,队列+邻接表优化)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。