题面

\(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的更多相关文章

  1. Luogu P5122 [USACO18DEC]Fine Dining 最短路

    先跑一遍n为起点最短路,再新开一个点,向有干草垛的点连一根边权为d[u]-w的有向边(很重要..我当时连的无向边,然后我死了.),相当于用价值抵消一部分边权, 然后以这个新的点为起点跑最短路就好了.. ...

  2. 题解 P5122 【[USACO18DEC]Fine Dining】

    思路:最短路+dp 1.先跑一遍最短路,计算出没有干草垛最少要多少时间 2.dp求出有干草垛至少需要多少时间,由于dp有后效性,所以用SPFA辅助转移,dp方程和求最短路一模一样,只是先将有干草垛的拉 ...

  3. [题解](最短路)luogu_P5122 Fine Dining

    首先理解这里的美味值相当于给你更多时间让你经过这个草垛的, 也就是在经过草垛时可以给你的时间减少w[i],这样能否比最短路不慢 然而我们并不容易知道怎么走才是最好的,所以要想办法避免找这个方案 我们新 ...

  4. FOOD

    Serving order of food courses(上菜顺序)1. Appetizer(starter)2. Main Course3. Dessert Style of cooking1. ...

  5. USACO比赛题泛刷

    随时可能弃坑. 因为不知道最近要刷啥所以就决定刷下usaco. 优先级排在学习新算法和打比赛之后. 仅有一句话题解.难一点的可能有代码. 优先级是Gold>Silver.Platinum刷不动. ...

  6. [USACO 2018 December Contest]作业总结

    t1 Convention 题目大意 每一头牛都有一个来的时间,一共有\(n\)辆车,求出等待时间最长的那头牛等待的最小时间. 解法 第一眼看到这道题还以为是\(2018noip\)普及组的t3魔鬼题 ...

  7. 【托业】【新托业TOEIC新题型真题】学习笔记12-题库八-P7

    155.political figure 政治人物 prominent 160.association n.协会,社团; 联合,联系; 联想; rarely adv.很少地; 罕有地; 极精彩地; 珍 ...

  8. The 10 Best Neighborhoods in Seattle

    https://www.seattlemet.com/articles/2015/4/24/the-10-best-neighborhoods-in-seattle-may-2015 By Darre ...

  9. BZOJ 1711: [Usaco2007 Open]Dining吃饭

    1711: [Usaco2007 Open]Dining吃饭 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 902  Solved: 476[Submit ...

随机推荐

  1. 【luogu P2065 [TJOI2011]卡片】 假题解

    题目链接:https://www.luogu.org/problemnew/show/P2065 辣鸡匈牙利,没有优化贼鸡儿慢 // luogu-judger-enable-o2 #include & ...

  2. Android学习笔记_11_ListView控件使用

    一.界面设计: 1.activity_main.xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk ...

  3. 02_Linux 终端命令格式

    01. 终端命令格式 command [-options] [parameter] 说明: command:命令名,相应功能的英文单词或单词的缩写 [-options]:选项,可用来对命令进行控制,也 ...

  4. SpringBoot非官方教程 | 第十八篇: 定时任务(Scheduling Tasks)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot18-scheduling/ 本文出自方志朋的博客 ...

  5. SpringBoot非官方教程 | 第十二篇:springboot集成apidoc

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-apidoc/ 本文出自方志朋的博客 首先声明下 ...

  6. 协议类接口 - UART

    一.何为协议类接口? 双方约定信号的协议和满足时序要求. 二.UART如何传数据 通用异步收发器简称 UART,即“Universal Asynchronous Receiver Transmitte ...

  7. 【转】opatch学习

    [转自:https://yq.aliyun.com/articles/28007,仅作学习用途] Opatch 是oracle公司开发的安装,卸载,检测patch冲突的工具,管理oracle所有已经安 ...

  8. Qt基于model/view数据库编程3

    QSqlQueryModel和QSqlQuery类: 工程开发过程中将这两个类合起来使用,用QSqlQueryModel查询展示数据库中的数据,用QSqlQuery类执行sql语言,实现对数据库的操作 ...

  9. C++ primer 练习9.52 适配器stack 中缀表达式

    //调试环境 VS2015//本人菜鸟一枚,不喜勿喷! 谢谢!!!//主要思想引自  http://www.cnblogs.com/dolphin0520/p/3708602.html//主要代码引自 ...

  10. chromium之tracked_objects

    // For each thread, we have a ThreadData that stores all tracking info generated // on this thread. ...