洛谷 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. bootstrap作为mixin库的应用模式

    Bootstrap作为一个非常流行的前端css框架,得到了非常多的应用.一般的使用方法都是直接download bootstrap.css,作为css文件引入到html的markup中,随后直接引用其 ...

  2. Vue2学习笔记:数据交互vue-resource

    基本语法 必须引入一个库:vue-resource github地址 // 基于全局Vue对象使用http Vue.http.get('/someUrl', [options]).then(succe ...

  3. Ubuntu安装 和 python开发

    在ubuntu上安装pycharm 可以好几种下载办法 1.pycharm之linux版本下载地址: https://download.jetbrains.8686c.com/python/pycha ...

  4. [翻译] ZCSHoldProgress

    ZCSHoldProgress 以下是使用效果: https://github.com/zshannon/ZCSHoldProgress "Your users be pressin' lo ...

  5. Electron简介和安装使用

    一.Electron简介Electron的应用,使得了JS不仅仅可以BS端项目,还可以做CS端项目. 另外今天之所以研究下Electron,还是因为公司的需要,业务场景是,通过Electron.js开 ...

  6. bat 数组实现

    bat中没有数组的概念,可以通过有[]的多个变量来存储一组值 @echo off & setlocal enabledelayedexpansion .txt) do ( echo %%b e ...

  7. 纯CSS + 媒体查询实现网页导航特效

    纯css+媒体查询实现网页导航特效 附上效果图: 代码如下,复制即可使用: <!DOCTYPE html> <html lang="en"> <hea ...

  8. UITableViewCell的父视图

    最近版本测试阶段,发现一个奇怪的问题,以前在A测试机上出现的崩溃bug,解决后今天在B测试机上又出现了,在B上解决完之后,返回到设备A上发现又不行了.最后调试发现是测试设备系统版本不同导致的,A设备是 ...

  9. Angular7教程-01-Angular开发环境配置

    本教程基于angular7(2018-11-04) 1. 安装node.js 下载地址: http://nodejs.cn/download/ 下载对应自己操作系统的版本安装即可. 2.安装 angu ...

  10. 名字&值

    1)名字VS值 名字和内存(存储)位置相关联. 名字—(环境)———>位置——(状态)——>值 这两个映射都在随着程序的运行而改变. 2)环境VS状态 环境是指一个名字到存储位置映射,也可 ...