题目链接

题目

题目描述

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.

示例1

输入

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.

题解

知识点:最短路。

第一种路不需要任何花费,第二种路花费 \(T_i\) 。另外到达每个城市都能赚 \(D\) ,因此考虑每条边都加上 \(D\)。并且最后答案额外加一次 \(D\) ,因为起点城市也要算进去。

最后跑SPFA最长路,因为存在正权。

时间复杂度 \(O(k(P+F))\)

空间复杂度 \(O(C+P+F)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; const int N = 307, M = 507; int D, P, C, F, S; template<class T>
struct Graph {
struct edge {
int v, nxt;
T w;
};
int idx;
vector<int> h;
vector<edge> e; Graph(int n, int m) :idx(0), h(n + 1), e(m + 1) {} void clear(int n, int m) {//全局使用时清零,确定范围防止超时
idx = 0;
h.assign(n + 1, 0);
e.assign(m + 1, { 0,0,0 });
} void add(int u, int v, T w) {
e[++idx] = edge{ v,h[u],w };
h[u] = idx;
}
};
Graph<int> g(N, M); bool vis[N];
int dis[N], cnt[N];
queue<int> q;
bool SPFA(int st) {
memset(dis, -0x3f, sizeof(dis));
q.push(st);
vis[st] = 1;
dis[st] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = 0;
for (int i = g.h[u];i;i = g.e[i].nxt) {
int v = g.e[i].v, w = g.e[i].w;
if (dis[v] < dis[u] + w) {
dis[v] = dis[u] + w;
cnt[v] = cnt[u] + 1;
if (cnt[v] >= C) return false;
if (!vis[v]) {
vis[v] = 1;
q.push(v);
}
}
}
}
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> D >> P >> C >> F >> S;
for (int i = 1;i <= P;i++) {
int A, B;
cin >> A >> B;
g.add(A, B, D);
}
for (int i = 1;i <= F;i++) {
int J, K, T;
cin >> J >> K >> T;
g.add(J, K, D - T);
}
int ans = -1;
if (SPFA(S)) {
for (int i = 1;i <= C;i++) ans = max(ans, dis[i]);
}
cout << ans + D << '\n';
return 0;
}

NC24858 [USACO 2009 Nov S]Job Hunt的更多相关文章

  1. BZOJ2017[USACO 2009 Nov Silver 1.A Coin Game]——DP+博弈论

    题目描述 农夫约翰的奶牛喜欢玩硬币游戏,因此他发明了一种称为“Xoinc”的两人硬币游戏. 初始时,一个有N(5 <= N <= 2,000)枚硬币的堆栈放在地上,从堆顶数起的第I枚硬币的 ...

  2. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  3. USACO 2009 Open 干草塔 Tower of Hay

    USACO 2009 Open 干草塔 Tower of Hay Description 为了调整电灯亮度,贝西要用干草包堆出一座塔,然后爬到牛棚顶去把灯泡换掉.干草 包会从传送带上运来,共会出现N包 ...

  4. USACO 2009 Feb 股票市场 Stock Market

    USACO 2009 Feb 股票市场 Stock Market Description 尽管奶牛们天生谨慎,她们仍然在住房抵押信贷市场中大受打击,现在她们准备在股市 上碰碰运气.贝西开挂了,她知道S ...

  5. 【Usaco 2009 Gold】JZOJ2020年9月19日提高B组T4 过路费

    [Usaco 2009 Gold]JZOJ2020年9月19日提高B组T4 过路费 题目 Description 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生财之 ...

  6. 【Usaco 2009 Gold】JZOJ2020年9月19日提高B组T3 头晕的奶牛

    [Usaco 2009 Gold]JZOJ2020年9月19日提高B组T3 头晕的奶牛 题目 Description 奶牛们发现,在农场里面赛跑是很有趣的一件事.可是她们一旦在农场里面不断地转圈,就会 ...

  7. 【Usaco 2009 Gold 】JZOJ2020年9月19日提高B组T2 电视游戏问题

    [Usaco 2009 Gold ]JZOJ2020年9月19日提高B组T2 电视游戏问题 题目 Description 农夫约翰的奶牛们游戏成瘾!本来FJ是想要按照陶叫兽的做法拿她们去电击戒瘾的,可 ...

  8. 【Usaco 2009 Silver】JZOJ2020年9月19日提高B组T1 音乐节拍

    [Usaco 2009 Silver]JZOJ2020年9月19日提高B组T1 音乐节拍 题目 Description FJ准备教他的奶牛弹奏一首歌曲,歌曲由N(1<=N<=50,000) ...

  9. NC24840 [USACO 2009 Mar S]Look Up

    NC24840 [USACO 2009 Mar S]Look Up 题目 题目描述 Farmer John's N (1 <= N <= 100,000) cows, convenient ...

  10. NC24866 [USACO 2009 Dec S]Music Notes

    NC24866 [USACO 2009 Dec S]Music Notes 题目 题目描述 FJ is going to teach his cows how to play a song. The ...

随机推荐

  1. Redis 常用五种数据类型编码

    转载请注明出处: 目录 Redis 的五种数据结构 Redis 数据结构的内部编码 1.String 1.1 常用命令 1.2 内部编码 1.3 典型使用场景 2. Hash 2.1 常用命令及时间复 ...

  2. SV 字符串类型

    概述 常见使用方式 string b; string b=""; // 拼接字符串 string a = {"hi",b}; // 将字符串a赋值给[15:0] ...

  3. [转帖]一文带你了解mysql sql model的only_full_group_by模式

    https://zhuanlan.zhihu.com/p/368440685 Mysql only_full_group_by与Error 1055问题分析 1 声明 本文的数据来自网络,部分代码也有 ...

  4. tidb备份恢复的方式方法

    tidb备份恢复的方式方法 摘要 可以单独每个数据库实例进行备份,但是这种机制实在是太慢了. 网上查资料发现可以使用 tiup br 的方式进行备份. 但是大部分文档都比较陈旧, 官网上面又比较贴心的 ...

  5. [转帖]TiDB 热点问题处理

    TiDB 热点问题处理 本文介绍如何定位和解决读写热点问题. TiDB 作为分布式数据库,内建负载均衡机制,尽可能将业务负载均匀地分布到不同计算或存储节点上,更好地利用上整体系统资源.然而,机制不是万 ...

  6. [转帖]自动化配置SSH免密登录和取消SSH免密配置脚本

    1. 前文 搭建了一套有多台主机的局域网环境,不完全考虑安全性的情况下,为方便管理局域网内主机,配置SSH免密登录,因主机较多,前阵子针对配置ssh免密和取消ssh免密功能单独写了脚本来自动化批量部署 ...

  7. s-tui验证机器主频的过程

    摘要 小年在家陪孩子. 翻阅<企业存储技术>公众号的文章时 找到了 s-tui 进行监控机器主频的文章 感觉挺有用的 想验证一下 虚拟机有否支持Intel的睿频功能. 所以将之前写的pyt ...

  8. VictoriaMetrics 1.80版本中值得关注的新特性

    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 change log请看:https://github.c ...

  9. Gin 框架之jwt 介绍与基本使用

    目录 一.JWT 介绍 二.JWT认证与session认证的区别 2.1 基于session认证流程图 2.2 基于jwt认证流程图 三. JWT 的构成 3.1 header : 头部 3.2 pa ...

  10. navicat连接远程docker中的mysql报错解决

    总是报错 填写主机地址有错误,终于找到方法,是因为docker中的mysql没有设置ip地址,navicat不识别,奉上 https://blog.csdn.net/qq_42838723/artic ...