首页 > 其他分享 >1014 [USACO 2009 Nov S]Job Hunt spfa 负权 最长路 判断是否可以不断变大

1014 [USACO 2009 Nov S]Job Hunt spfa 负权 最长路 判断是否可以不断变大

时间:2022-08-18 19:56:41浏览次数:88  
标签:city int make Hunt cin USACO spfa she Bessie

 链接: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.

输入描述:

* 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

输入

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

输出

复制
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.

分析

最长路

将每个点看成每条边

如果是公路权值就是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;
}

/*样例区


*/

//------------------------------------------------------------

 

标签:city,int,make,Hunt,cin,USACO,spfa,she,Bessie
From: https://www.cnblogs.com/er007/p/16599895.html

相关文章

  • 1013 [USACO 2007 Ope S]Catch That Cow bfs 剪枝
     链接:https://ac.nowcoder.com/acm/contest/26077/1013来源:牛客网题目描述FarmerJohnhasbeeninformedofthelocationofafugitivecow......
  • CF1027D.Mouse Hunt
    题目:花费最少逮老鼠分析:每个出度为0的强连通分量放置捕鼠器。1#include<iostream>2#include<algorithm>3#include<cstring>4#include<string>5#inclu......
  • P3120 [USACO15FEB]Cow Hopscotch G
    传送门思路朴素的想法就是一个\(O(n^2m^2)\)的转移:\[f_{i,j}=\sum_{x=1}^{i-1}\sum_{y=1}^{j-1}f_{x,y}*[a_{i,j}!=a_{x,y}]\]约束条件如此多,思考用cdq分治来优化......
  • P7154 [USACO20DEC] Sleeping Cows P(DP)
    主要是状态设计比较难想,但其实可以理性地推出来。P7154[USACO20DEC]SleepingCowsP考虑最终一个合法状态是怎么样的:一定是一堆小牛棚,一堆大奶牛,最大的牛棚小于最小的......
  • P6144 [USACO20FEB]Help Yourself P(DP+线段树)
    P6144[USACO20FEB]HelpYourselfP将线段按照了\(r\)排序,设右端点为\(r\)的答案为\(f_r\),发现这样转移非常困难。\(\color{yellow}{\bigstar\texttt{Trick}}\):区间......
  • NC24870 [USACO 2009 Dec G]Video Game Troubles
    题目链接题目题目描述FarmerJohn'scowslovetheirvideogames!FJnoticedthatafterplayingthesegamesthathiscowsproducedmuchmoremilkthanusual,s......