洛谷 1938 [USACO09NOV]找工就业Job Hunt
洛谷 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。
输出格式:
一个整数,在上述规则下最多可以赚到的钱数。
输入输出样例
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.
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的更多相关文章
- 洛谷 P1938 [USACO09NOV] 找工就业Job Hunt
这道题可以说是一个复活SPFA的题 因为数据比较小,SPFA也比较简单 那就复习(复读)一次SPFA吧 #include<iostream> #include<cstdio> ...
- P1938 [USACO09NOV]找工就业Job Hunt
P1938 [USACO09NOV]找工就业Job Hunt给边赋予价值,入边的权值为D-Ti,然后从起点开始跑最长路,如果钱的总数超过了D*C,也就是一定有一个城市走了两遍,则有正环,则输出-1 # ...
- 题解【洛谷P1938】 [USACO09NOV]找工就业Job Hunt
题面 题解 将路径连边\((x, y, d)\) ,将航线连边\((x, y, d - w)\).其中线路是从\(x\)到\(y\),航线的费用为\(w\),\(d\)的含义如题面. 跑一遍\(SPF ...
- luogu P1938 [USACO09NOV]找工就业Job Hunt
题目描述 奶牛们正在找工作.农场主约翰知道后,鼓励奶牛们四处碰碰运气.而且他还加了一条要求:一头牛在一个城市最多只能赚D(1≤D≤1000)美元,然后它必须到另一座城市工作.当然,它可以在别处工作一阵 ...
- [Luogu1938][USACO09NOV]找工就业Job Hunt
原题链接:https://www.luogu.org/problem/show?pid=1938 这一道题有一个比较难的点就是,这一张图上,是点上有权.既然点上有权的话,我们就不好一下子使用最短路了. ...
- 洛谷P3412 仓鼠找$Sugar\ II$题解(期望+统计论?)
洛谷P3412 仓鼠找\(Sugar\ II\)题解(期望+统计论?) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1327573 原题链接:洛谷P3412 ...
- 洛谷p3398仓鼠找suger题解
我现在爱死树链剖分了 题目 具体分析的话在洛谷blog里 这里只是想放一下改完之后的代码 多了一个son数组少了一个for 少了找size最大的儿子的for #include <cstdio&g ...
- 洛谷P1938 找工就业
传送门啦 这个题本质就是跑一边最长路,重点就是在怎么建图上. 我们可以把点权放到边权上面,即将每一个边的终点点权当做这个边的边权,这个题里就是将工钱 $ d $ 当做边权. 如果这一条边需要坐飞机才能 ...
- 洛谷P3398 仓鼠找sugar [LCA]
题目传送门 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而 ...
随机推荐
- mblog相关
Mblog mblog(mini blog) 是一个用Java实现的多人博客, 使用 mysql 数据库. 使用的框架: Bootstrap 3 Spring mvc Velocity Hiberna ...
- Tomcat6.0下的jsp、servlet和javabean的配置
第一步:下载jdk和tomcat: 第二步:安装和配置你的jdk和tomcat:执行jdk和tomcat的安装程序,然后设置按照路径进行安装即可.1.安装jdk以后,需要配置一下环境变量,在我的电脑- ...
- spring boot 在jdk 1.7下使用 commandLineRunner
在spring boot 中有一段代码,使用的是java 1.8的语法: @Bean public CommandLineRunner commandLineRunner(ApplicationCon ...
- 零基础图文傻瓜教程接入Facebook的sdk
零基础图文傻瓜教程接入Facebook的sdk 本人视频教程系类 iOS中CALayer的使用 0. 先解决你的 VPN FQ上外网问题,亲,解决这一步才能进行后续操作^_^. 1. 点击右侧链接 ...
- Latex排版全解
Latex排版全解 LATEX(英语发音:/ˈleɪtɛk/ LAY-tek或英语发音:/ˈlɑːtɛk/ LAH-tek,音译“拉泰赫”),是一种基于TEX的排版系统,由美国电脑学家莱斯利•兰伯特在 ...
- Nginx总结.md
基本配置 注意:下面的nginx版本是1.10,安装是在CentOS 7中通过epel源进行安装的nginx默认配置文件. # egrep -v "(^$)|(^#)|#" /et ...
- [SDOI2017]切树游戏
题目 二轮毒瘤题啊 辣鸡洛谷竟然有卡树剖的数据 还是\(loj\)可爱 首先这道题没有带修,设\(dp_{i,j}\)表示以\(i\)为最高点的连通块有多少个异或和为\(j\),\(g_{i,j}=\ ...
- Day4 数组
双重for循环 外循环控制行,内循环控制列. //乘法表 ; i <= ; i++) { ; j <= i ;j++) { System.out.print(j+"*" ...
- docker 部署 redmine 项目管理软件
最近部署一套redmine项目管理程序, ruby部署各种问题,用docker 直接run, 简单方便. . docker run --name=mysql-redmine -d -p : -v /d ...
- virtualbox+vagrant学习-2(command cli)-24-Aliases别名
Aliases 别名的部分灵感来自Git自身的别名功能,它允许你创建自己的定制vagrant命令,从而使你的vagrant体验更简单.更容易.更熟悉. 别名可以在VAGRANT_HOME/ alias ...