题目来源:

点击打开链接

题目描述:

题目描述

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 十一月银组

解题思路:

这题是一个最长路的裸题,关键是建边,假设起点是s,s有到a的单向路径,那么就建一条从s到a边权为d的有向边,如果是单向航线,那么就建一条从s到a边权为d-c,c为航费,然后跑一便最长路就行,最后把答案加上一个d(s的)就行,或者可以将边权都取反,那么就只要求最短路就行。。。代码是求最短路。

代码:

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
using namespace std;
vector<pair<int,int> >E[1000];
queue<int>q;
bool vis[1000];int dis[1000];
int rd[1000];
int main()
{int d,m1,n,m2,s;cin>>d>>m1>>n>>m2>>s;for(int i=1;i<=m1;i++){int a,b;cin>>a>>b;E[a].push_back(make_pair(b,-d));}for(int i=1;i<=m2;i++){int a,b,c;cin>>a>>b>>c;E[a].push_back(make_pair(b,c-d));}memset(vis,0,sizeof(vis));memset(rd,0,sizeof(rd));memset(dis,inf,sizeof(dis));int flag=0;q.push(s);vis[s]=1;rd[s]++;dis[s]=0;while(!q.empty()){int now=q.front();q.pop();vis[now]=0;if(rd[now]>n){flag=1;break;}for(int i=0;i<E[now].size();i++){int v=E[now][i].first;//cout<<dis[v]<<" "<<dis[now]+E[now][i].second<<endl;if(dis[v]>dis[now]+E[now][i].second){dis[v]=dis[now]+E[now][i].second;//   cout<<dis[v]<<endl;if(!vis[v]){q.push(v);vis[v]=1;rd[v]++;}}}}if(flag)cout<<"-1"<<endl;else {int ans=inf;for(int i=1;i<=n;i++)if(dis[i]<ans){ans=dis[i];}cout<<(-1)*ans+d<<endl;}return 0;
}

洛谷P1938 [USACO09NOV]找工就业Job Hunt(spfa) 题解相关推荐

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

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

  2. [洛谷P3181] [HAOI2016]找相同字符

    洛谷题目链接:[HAOI2016]找相同字符 题目描述 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两个子串中有一个位置不同. 输入输出格式 输入 ...

  3. 洛谷入门1【顺序结构】题单题解

    目录 超级玛丽 字母转换 数字反转 再分肥皂水 小鱼的游泳时间 小学数学N合1 三角形面积 苹果和虫子 对角线 上学迟到 [入门1]顺序结构 - 题单 - 洛谷 | 计算机科学教育新生态 (luogu ...

  4. 洛谷P1938 找工就业

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

  5. 折半搜索+洛谷 P2962 [USACO09NOV]Lights G

    题意: 有 n盏灯,每盏灯与若干盏灯相连,每盏灯上都有一个开关,如果按下一盏灯上的开关,这盏灯以及与之相连的所有灯的开关状态都会改变.一开始所有灯都是关着的,你需要将所有灯打开,求最小的按开关次数.( ...

  6. 洛谷 P2962 [USACO09NOV]灯Lights

    P2962 [USACO09NOV]灯Lights 题目描述 Bessie and the cows were playing games in the barn, but the power was ...

  7. 洛谷 P3398 仓鼠找 sugar

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

  8. 洛谷 3398 仓鼠找sugar 【模板】判断树上两链有交

    [题解] 题意就是判断树上两条链是否有交.口诀是"判有交,此链有彼祖".即其中一条链的端点的Lca在另一条链上. 我们设两条链的端点的Lca中深度较大的为L2,对L2与另一条链的两 ...

  9. 洛谷 P3244 / loj 2115 [HNOI2015] 落忆枫音 题解【拓扑排序】【组合】【逆元】

    组合计数的一道好题.什么非主流题目 题目背景 (背景冗长请到题目页面查看) 题目描述 不妨假设枫叶上有 \(n​\) 个穴位,穴位的编号为 \(1\sim n​\).有若干条有向的脉络连接着这些穴位. ...

  10. 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond(spfa+最短路计数) 题解

    题目来源: https://www.luogu.org/problemnew/show/P1606 题目描述: 题目描述 FJ has installed a beautiful pond for h ...

最新文章

  1. Python解决ModuleNotFoundError: No module named ‘Queue‘的问题
  2. nssl1478-题【dp】
  3. 【读书笔记】非暴力沟通
  4. python字符画太小_python小项目(-)图片转字符画
  5. Qt 自定义事件的实现
  6. C#中ToString()格式详解
  7. python中int函数是什么作用_python中int函数怎么用
  8. Deep Learning Tutorial - Multilayer perceptron
  9. OpenCV-图像处理(18、Laplance算子)
  10. PPT模板 | 红色学术风论文答辩PPT模板
  11. pdf.js根据路径里传参数高亮显示关键字(跳转到对应页面)
  12. 2021年连云港高考成绩查询,2021年连云港高考状元是谁分数多少分,历年连云港高考状元名单...
  13. 中国首台云电脑全面解析——天霆云计算董事长谈天霆专访
  14. 视频大数据与物联网(IoT)融合发展的探索
  15. 斗鱼直播与熊猫直播竞品分析
  16. 刷屏器!简单!快速!稳定!可控制速度!
  17. Python自动化之Excel去除相同数据
  18. cordova app强制横屏
  19. wordpress一次性删除草稿和回收站文章代码
  20. JavaScript连载20-数据存储方式、内置对象Array详解

热门文章

  1. 计算机系统概论第2版答案第七章,计算机系统概论(第七章).ppt
  2. XAMPP下的Tomcat 7运行出现“1% 不是有效的 Win32 应用程序。”
  3. springboot JWT Token 自动续期的解决方案
  4. Java毕设项目晨光文具店进销存系统设计与开发计算机(附源码+系统+数据库+LW)
  5. Hive分析窗口函数: LAG、LEAD、FIRST_VALUE、LAST_VALUE
  6. html用360打不开,win7系统使用360安全卫士解决浏览器打不开的方法
  7. GeForce Experience界面重叠解决办法
  8. [Java web编程]第2章 HTML与css网页开发基础(动画)
  9. HTK中Vocab字典的结构
  10. 【houdini hom】弹砖块