[USACO18DEC]Fine Dining
题面
\(Solution:\)
一开始想的是先跑一遍最短路,然后拆点之后再跑一遍,比较两次dis,然后发现拆点后会有负环(可能是我没想对拆点的方法),于是就放弃了拆点法。
我们考虑强制让每头牛选择走一条最短的,有草堆的路径,然后比较单纯的最短路。
然后就想到了分层图,在每一个有草垛的点向第二维图对应的点连一条单向的,权值为-美味值的边,这样第二维图上的dis就是每头牛选择走一条最短,有草堆的路径长度,再和第一维比较即可.注意有负边,跑某死亡算法.
\(Source\)
#include <set>
#include <queue>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <assert.h>
#include <algorithm>
using namespace std;
#define fir first
#define sec second
#define pb push_back
#define mp make_pair
#define LL long long
#define INF (0x3f3f3f3f)
#define mem(a, b) memset(a, b, sizeof (a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(x) cout << #x << " = " << x << endl
#define travle(i, x) for (register int i = head[x]; i; i = nxt[i])
#define For(i, a, b) for (register int (i) = (a); (i) <= (b); ++ (i))
#define Forr(i, a, b) for (register int (i) = (a); (i) >= (b); -- (i))
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define ____ debug("go\n")
namespace io {
static char buf[1<<21], *pos = buf, *end = buf;
inline char getc()
{ return pos == end && (end = (pos = buf) + fread(buf, 1, 1<<21, stdin), pos == end) ? EOF : *pos ++; }
inline int rint() {
register int x = 0, f = 1;register char c;
while (!isdigit(c = getc())) if (c == '-') f = -1;
while (x = (x << 1) + (x << 3) + (c ^ 48), isdigit(c = getc()));
return x * f;
}
inline LL rLL() {
register LL x = 0, f = 1; register char c;
while (!isdigit(c = getc())) if (c == '-') f = -1;
while (x = (x << 1ll) + (x << 3ll) + (c ^ 48), isdigit(c = getc()));
return x * f;
}
inline void rstr(char *str) {
while (isspace(*str = getc()));
while (!isspace(*++str = getc()))
if (*str == EOF) break;
*str = '\0';
}
template<typename T>
inline bool chkmin(T &x, T y) { return x > y ? (x = y, 1) : 0; }
template<typename T>
inline bool chkmax(T &x, T y) { return x < y ? (x = y, 1) : 0; }
}
using namespace io;
const int N = 5e4 + 2, M = 1e5 + 2;
int n, m, k;
int dis[N<<1];
namespace Gragh {
int head[N<<1], ver[(M<<2) + N], nxt[(M<<2) + N], tot, edge[(M<<2) + N];
inline void add(int u, int v, int w) {
ver[++tot] = v, edge[tot] = w, nxt[tot] = head[u], head[u] = tot;
}
} using namespace Gragh;
bool vis[N<<1];
void SPFA(int st) {
queue<int> q;
memset(dis, 0x3f, sizeof dis);
q.push(st);
dis[st] = 0;
vis[st] = 1;
while (q.size()) {
int u = q.front(); q.pop(); vis[u] = 0;
for (register int i = head[u]; i; i = nxt[i]) {
if (dis[ver[i]] > dis[u] + edge[i]) {
dis[ver[i]] = dis[u] + edge[i];
if (!vis[ver[i]]) {
vis[ver[i]] = 1;
q.push(ver[i]);
}
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
file("Fine_Dining");
#endif
n = rint(), m = rint(), k = rint();
For (i, 1, m) {
int u = rint(), v = rint(), w = rint();
add(u, v, w); add(v, u, w);
add(u + n, v + n, w); add(v + n, u + n, w);
}
For (i, 1, k) {
int u = rint(), val = rint();
add(u, u + n, -val);
}
SPFA(n);
for (register int i = 1; i < n; ++ i) if (dis[i] >= dis[i + n]) {
puts("1");
} else puts("0");
}
[USACO18DEC]Fine Dining的更多相关文章
- Luogu P5122 [USACO18DEC]Fine Dining 最短路
先跑一遍n为起点最短路,再新开一个点,向有干草垛的点连一根边权为d[u]-w的有向边(很重要..我当时连的无向边,然后我死了.),相当于用价值抵消一部分边权, 然后以这个新的点为起点跑最短路就好了.. ...
- 题解 P5122 【[USACO18DEC]Fine Dining】
思路:最短路+dp 1.先跑一遍最短路,计算出没有干草垛最少要多少时间 2.dp求出有干草垛至少需要多少时间,由于dp有后效性,所以用SPFA辅助转移,dp方程和求最短路一模一样,只是先将有干草垛的拉 ...
- [题解](最短路)luogu_P5122 Fine Dining
首先理解这里的美味值相当于给你更多时间让你经过这个草垛的, 也就是在经过草垛时可以给你的时间减少w[i],这样能否比最短路不慢 然而我们并不容易知道怎么走才是最好的,所以要想办法避免找这个方案 我们新 ...
- FOOD
Serving order of food courses(上菜顺序)1. Appetizer(starter)2. Main Course3. Dessert Style of cooking1. ...
- USACO比赛题泛刷
随时可能弃坑. 因为不知道最近要刷啥所以就决定刷下usaco. 优先级排在学习新算法和打比赛之后. 仅有一句话题解.难一点的可能有代码. 优先级是Gold>Silver.Platinum刷不动. ...
- [USACO 2018 December Contest]作业总结
t1 Convention 题目大意 每一头牛都有一个来的时间,一共有\(n\)辆车,求出等待时间最长的那头牛等待的最小时间. 解法 第一眼看到这道题还以为是\(2018noip\)普及组的t3魔鬼题 ...
- 【托业】【新托业TOEIC新题型真题】学习笔记12-题库八-P7
155.political figure 政治人物 prominent 160.association n.协会,社团; 联合,联系; 联想; rarely adv.很少地; 罕有地; 极精彩地; 珍 ...
- The 10 Best Neighborhoods in Seattle
https://www.seattlemet.com/articles/2015/4/24/the-10-best-neighborhoods-in-seattle-may-2015 By Darre ...
- BZOJ 1711: [Usaco2007 Open]Dining吃饭
1711: [Usaco2007 Open]Dining吃饭 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 902 Solved: 476[Submit ...
随机推荐
- CodeForces - 600B Queries about less or equal elements (二分查找 利用stl)
传送门: http://codeforces.com/problemset/problem/600/B Queries about less or equal elements time limit ...
- JVM 监控以及内存分析
1 内存分析1.1 jmap -histo 命令pid=`jps | awk '{if ($2 == "Jps") print $1}'`jmap -histo $pid > ...
- mysql replace()用法
mysql replace实例说明: UPDATE tb1 SET f1=REPLACE(f1, 'abc', 'def'); 释:表tb1中字段f1中为abc的值更新为def.一般用于某字段中值存在 ...
- 用c#语言编写水仙花数
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 零基础Python知识点回顾(三)
元组 元组是用圆括号括起来的,其中的元素之间用逗号隔开.(都是英文半角)tuple(元组)跟列表类似是一种序列类型的数据,特点就是其中的元素不能更改 既然是有序的,那么,嘿嘿,不错,它也可以有索引,能 ...
- JavaScript监控输入框字数变化,超出限制则禁止输入
JavaScript监控输入框字数变化,超出则禁止输入 不废话,给你看看效果: 1.无输入状态: 2.输入三个字符: 3.超出5个后报错: 现在粘出代码,首先是html代码: <body> ...
- 解读JavaScript中的Hoisting机制(js变量声明提升机制)
hoisting机制:javascript的变量声明具有hoisting机制,JavaScript引擎在执行的时候,会把所有变量的声明都提升到当前作用域的最前面. 知识点一:javascript是没有 ...
- VUE插件整理
转自:https://blog.csdn.net/miaozhenzhong/article/details/80138174 1.VsCode官方插件地址: https://marketplace. ...
- C++继承和派生练习(一)--关于vehicle基类
Target:定义一个车(vehicle)基类 具有MaxSpeed.Weight等成员变量,Run.Stop等成员函数,由此派生出自行车(bicycle)类.汽车(motorcar)类. 自行车(b ...
- httpd的prefork、worker、event
Apache(httpd) 有3种核心MPM(Multi-Processing Module,多进程处理模块)工作模式,分别是prefork,worker和event,其中httpd-2.2的even ...