洛谷 1938  [USACO09NOV]找工就业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.

奶牛们正在找工作。农场主约翰知道后,鼓励奶牛们四处碰碰运气。而且他还加了一条要求:一头牛在一个城市最多只能赚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 十一月银组

题解:

可以把每条边的花费看成负数,更新每个点的最长路时加上这个点的点权。如果出现环,那么肯定可以赚无限多的钱。建图时将航线的边权定为花费,没用航线的路线花费就是0了。 由于需要判断是否有环,所以需要用到spfa。找最长路啊。

代码:

#include<cstdio>
#include<queue>
const int inf=;
struct Edge {
int to,next,v;
} e[];
int head[],dis[],time[],d,p,c,f,s,nxt,ans=-;;
std::queue<int>q;
bool vis[];
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=; i<=c; i++)
dis[i]=-inf;
dis[s]=d,q.push(s),vis[s]=;
while(!q.empty()) {
int u=q.front();
q.pop(),vis[u]=;
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]=;
}
}
}
}
for(int i=; 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=; i<=p; i++)
scanf("%d%d",&x,&y),add(x,y,);
for(int i=,x,y,z; i<=f; i++)
scanf("%d%d%d",&x,&y,&z),add(x,y,z);
spfa();
printf("%d",ans);
return ;
}

AC

洛谷 1938 [USACO09NOV]找工就业Job Hunt的更多相关文章

  1. 洛谷 P1938 [USACO09NOV] 找工就业Job Hunt

    这道题可以说是一个复活SPFA的题 因为数据比较小,SPFA也比较简单 那就复习(复读)一次SPFA吧 #include<iostream> #include<cstdio> ...

  2. P1938 [USACO09NOV]找工就业Job Hunt

    P1938 [USACO09NOV]找工就业Job Hunt给边赋予价值,入边的权值为D-Ti,然后从起点开始跑最长路,如果钱的总数超过了D*C,也就是一定有一个城市走了两遍,则有正环,则输出-1 # ...

  3. 题解【洛谷P1938】 [USACO09NOV]找工就业Job Hunt

    题面 题解 将路径连边\((x, y, d)\) ,将航线连边\((x, y, d - w)\).其中线路是从\(x\)到\(y\),航线的费用为\(w\),\(d\)的含义如题面. 跑一遍\(SPF ...

  4. luogu P1938 [USACO09NOV]找工就业Job Hunt

    题目描述 奶牛们正在找工作.农场主约翰知道后,鼓励奶牛们四处碰碰运气.而且他还加了一条要求:一头牛在一个城市最多只能赚D(1≤D≤1000)美元,然后它必须到另一座城市工作.当然,它可以在别处工作一阵 ...

  5. [Luogu1938][USACO09NOV]找工就业Job Hunt

    原题链接:https://www.luogu.org/problem/show?pid=1938 这一道题有一个比较难的点就是,这一张图上,是点上有权.既然点上有权的话,我们就不好一下子使用最短路了. ...

  6. 洛谷P3412 仓鼠找$Sugar\ II$题解(期望+统计论?)

    洛谷P3412 仓鼠找\(Sugar\ II\)题解(期望+统计论?) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1327573 原题链接:洛谷P3412 ...

  7. 洛谷p3398仓鼠找suger题解

    我现在爱死树链剖分了 题目 具体分析的话在洛谷blog里 这里只是想放一下改完之后的代码 多了一个son数组少了一个for 少了找size最大的儿子的for #include <cstdio&g ...

  8. 洛谷P1938 找工就业

    传送门啦 这个题本质就是跑一边最长路,重点就是在怎么建图上. 我们可以把点权放到边权上面,即将每一个边的终点点权当做这个边的边权,这个题里就是将工钱 $ d $ 当做边权. 如果这一条边需要坐飞机才能 ...

  9. 洛谷P3398 仓鼠找sugar [LCA]

    题目传送门 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而 ...

随机推荐

  1. zabbix之自动发现Tomcat多实例(第一种:已经部署完成,后续不再添加;第二种:后续或根据需要添加Tomcat实例)

    单一实例手动部署:https://www.cnblogs.com/huangyanqi/p/8522526.html 注释:参考的一位博主的博客后续做的修改,那个博主的网址找不到了!!!! 背景: 1 ...

  2. Springboot 设置session超时

    使用版本 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring ...

  3. December 02nd 2016 Week 49th Friday

    People will fall for its appearance while driving passionately. 观者倾心,驭者动魄. An advertisement of Merce ...

  4. [DAViCHi/SeeYa/T-ARA][원더우먼][Wonder Woman]

    歌词来源:http://music.163.com/#/song?id=5371229 作曲 : 赵英秀 [作曲 : 赵英秀] [作曲 : 赵英秀] 作词 : K-Smith [作词 : KSmith ...

  5. super深究

    super的入门使用: 在类的继承中,如果定义某个方法,该方法会覆盖父类的同名方法,但有时候我们希望能同时实现父类的功能,这时,我们就需要调用父类的方法了,可以通过使用super来实现.比如: cla ...

  6. JNI学习笔记

    JNI是什么->一套c和java的互掉规则   为什么使用JNI 1.非常多敏感效率的代码已经用C实现了 2. JNI双向.java调用c,c调用java   Java集成本地代码问题 1.代码 ...

  7. 看懂shebang吧,只需一点点shell知识,从此再也不犯强迫症

    Python2: 开启一个terminal,输入下面命令: yshuangj@ubuntu:~$ vim helloA.py 在vim编辑器中,进入编辑模式(按i),输入下面的代码,然后退出编辑模式( ...

  8. 从ByteBuffer中解析整数

    前言   在解析Redis返回的消息中,有类似 $5\r\nredis\r\n的数据返回,当我们解析这种数据的时候,先解析出5这个数字,然后在取后续的5长度的字符串.当时在解析数字这块卡住了,于是看了 ...

  9. python操作数据库(Mysql)

    原文地址:https://www.cnblogs.com/R-bear/p/7022231.html python DB-API介绍 1.python标准数据库接口为 python DB-API,py ...

  10. 关于 MFRC522引脚功能图

    MFRC522是属于13.56mhz芯片.另外SI522也是13.56mhz芯片,SI522 PIN对PIN完全兼容MFRC522,并且软硬件兼容,且引脚功能图都是一样的,功能方面比MFRC522多A ...