1014 [USACO 2009 Nov S]Job Hunt spfa 负权 最长路 判断是否可以不断变大
 链接:https://ac.nowcoder.com/acm/contest/26077/1014
来源:牛客网
题目描述
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 Ai to city Bi (1 <= Ai <= C; 1 <= Bi <= 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 Ji to a another Ki (1 <= Ji <= C; 1 <= Ki <= C) and which costs Ti (1 <= Ti <= 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.
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 Ai to city Bi (1 <= Ai <= C; 1 <= Bi <= 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 Ji to a another Ki (1 <= Ji <= C; 1 <= Ki <= C) and which costs Ti (1 <= Ti <= 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.
输入描述:
* Line 1: Five space-separated integers: D, P, C, F, and S
* Lines 2..P+1: Line i+1 contains two space-separated integers that name a one-way path from one city to another: Ai and Bi
* Lines P+2..P+F+1: Line P+i+1 contains three space-separated integers that name a one-way jet flight from one city to another and the price for that flight: Ji, Ki, and Ti
输出描述:
* Line 1: A single integer representing the most money she can make while following the law.
示例1
说明
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.
分析
最长路
将每个点看成每条边
如果是公路权值就是d
航班权值就是d - t
可能有负权,所以spfa或者bellman
如果某个点被放入队列的次数超过了总共的点数 - 1,说明可以不断变大,直接输出-1
//-------------------------代码---------------------------- //#define int ll const int N = 1e5+10; int n,m; int d,p,c,f,s; int e[N],ne[N],w[N],h[N],idx; void add(int a,int b,int c) { e[idx] =b,ne[idx] = h[a],w[idx] = c,h[a] = idx ++ ; } int dist[N]; int cnt[N]; bool vis[N]; void solve() { // cin>>n>>m; cin>>d>>p>>c>>f>>s; ms(h,-1); fo(i,1,p) { int a,b; cin>>a>>b;add(a,b,d); } fo(i,1,f) { int a,b,c;cin>>a>>b>>c; add(a,b,d-c); } priority_queue<pii> q; q.push({d,s}); dist[s] = d; cnt[s] ++ ; while(q.size()) { auto tmp = q.top();q.pop(); int dis = tmp.x,ver = tmp.y; if(cnt[ver] > c) { cout<<-1<<endl; rt; } vis[ver] = 0; fe(i,ver) { int j = e[i]; if(dist[j] < dist[ver] + w[i]) { dist[j] = dist[ver] + w[i]; cnt[j] ++ ; if(!vis[j]) { q.push({dist[j],j}); vis[j] = 1; } } } } int mx = 0; fo(i,1,c) { mx = max(mx,dist[i]); } cout<<mx<<endl; } void main_init() {} signed main(){ AC();clapping();TLE; cout<<fixed<<setprecision(12); main_init(); // while(cin>>n,n) // while(cin>>n>>m,n,m) // int t;cin>>t;while(t -- ) solve(); // {solve(); } return 0; } /*样例区 */ //------------------------------------------------------------
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号