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

样例输入
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(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。

思路:其实这个题仔细想想就是个最长路路加判断是否存在负环,Bell-Ford算法模板题,看代码
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
#define N 1010
int dis[N],u[N],v[N],w[N];
int d,p,c,f,s,k,m;
int main()
{
cin>>d>>p>>c>>f>>s;
m=p+f;
for(k=1;k<=p;k++)
cin>>u[k]>>v[k],w[k]=d;

for(;k<=m;k++)
{
cin>>u[k]>>v[k]>>w[k];
w[k]=d-w[k];
}
MEM(dis,128);
dis[s]=d;
for(int i=1;i<c;i++)
for(int j=1;j<=p+f;j++)
if(dis[v[j]]<dis[u[j]]+w[j])
dis[v[j]]=dis[u[j]]+w[j];

bool flag=false;
for(int j=1;j<=p+f;j++)
if(dis[v[j]]<dis[u[j]]+w[j])
flag=true;
if(flag)cout<<-1<<endl;
else {
int res=0;
for(int i=1;i<=c;i++)
res=max(res,dis[i]);
cout<<res<<endl;
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值