题面

\(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. 【办公-Word-VB】人民币大写转换-带完整注释

    完整代码见:我的CSDN博客 -------------------- 应公司财务人员的请求,需在Word中做个:输入阿拉伯数字,自动转换成大写,并填充到Word控件中对应的亿.万.千控件格子的功能, ...

  2. iOS 清除xcode缓存和生成文件

    方法1 按快捷键 shift+command+G 或者 Finder图标点击右键选 前往文件夹... 调出前往文件夹框 在里面输入如下 /Users/(自己电脑名字)/Library/Develope ...

  3. 读取静态的json文件

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  4. nginx 方向代理

    #nginx 监听原理 先监听端口 --> 再配置域名 -->匹配到就访问local 否则 没有匹配到域名就默认访问第一个监听端口的local地址 # vi nginx.conf user ...

  5. 纯 HTML5 APP与原生APP的差距在哪?

    纯 HTML5 APP与原生APP的差距在哪? 写过一些纯H5的APP,虽然开发起来的确很快很舒服,但和原生比起来纯H5APP还是有很多问题,主要聚集在以下几个方面: 1.动画 动画有很多种,比如侧边 ...

  6. c#一种存储结构解决动态平衡问题

    不说其他了,最近为了实现这么一个场景了而提取的一种结构.我们把一种数据缓存,比如开辟的存储Buffer,或者连接池.放置在一个结构中.很多时候这有一个共同的特点,我们的业务在一段时间会急剧增长,我们开 ...

  7. 爬虫——使用BeautifulSoup4的爬虫

    我们以腾讯社招页面来做示例:http://hr.tencent.com/position.php?&start=0#a 如上图,使用BeautifulSoup4解析器,将图1中229页,每页1 ...

  8. 爬虫——urllib.request库的基本使用

    所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地.在Python中有很多库可以用来抓取网页,我们先学习urllib.request.(在python2.x中为urllib2 ...

  9. Python 2.6.6升级到Python2.7.15

    最近在使用Python处理MySQL数据库相关问题时,需要用到Python2.7.5及以上版本,而centos6.5等版本操作系统默认自带的版本为2.6.6,因此需要对python进行升级. Pyth ...

  10. django的HttpResponse对象

    服务器接收到http协议的请求后,会根据报文创建HttpRequest对象,这个对象不需要我们创建,直接使用服务器构造好的对象就可以.视图的第一个参数必须是HttpRequest对象,在django. ...