洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party
银牛派对
正向建图+反向建图, 两边跑dijkstra,然后将结果相加即可。
反向建图以及双向建图的做法是学习图论的必备思想。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
//Mystery_Sky
//
#define maxn 1000010
#define maxm 5000050
#define INF 0x3f3f3f3f
struct Edge{
int next;
int w;
int to;
}edge1[maxn];
Edge edge2[maxn];
int n, m, X;
int head1[maxn], head2[maxn], cnt1, cnt2;
int vis1[maxn], vis2[maxn], dis1[maxn], dis2[maxn];
inline void add_edge1(int u, int v, int w)
{
edge1[++cnt1].to = v;
edge1[cnt1].next = head1[u];
edge1[cnt1].w = w;
head1[u] = cnt1;
}
inline void add_edge2(int u, int v, int w)
{
edge2[++cnt2].to = v;
edge2[cnt2].next = head2[u];
edge2[cnt2].w = w;
head2[u] = cnt2;
}
struct node{
int dis;
int pos;
inline bool operator <(const node &x) const
{
return x.dis < dis;
}
};
priority_queue <node> q1;
priority_queue <node> q2;
inline void dijkstra1()
{
dis1[X] = 0;
q1.push((node) {0, X});
while(!q1.empty()) {
node top = q1.top();
q1.pop();
int x = top.pos;
if(vis1[x]) continue;
vis1[x] = 1;
for(int i = head1[x]; i; i = edge1[i].next) {
int y = edge1[i].to;
if(dis1[y] > dis1[x] + edge1[i].w) {
dis1[y] = dis1[x] + edge1[i].w;
if(!vis1[y]) q1.push((node) {dis1[y], y});
}
}
}
}
inline void dijkstra2()
{
dis2[X] = 0;
q2.push((node) {0, X});
while(!q2.empty()) {
node top = q2.top();
q2.pop();
int x = top.pos;
if(vis2[x]) continue;
vis2[x] = 1;
for(int i = head2[x]; i; i = edge2[i].next) {
int y = edge2[i].to;
if(dis2[y] > dis2[x] + edge2[i].w) {
dis2[y] = dis2[x] + edge2[i].w;
if(!vis2[y]) q2.push((node) {dis2[y], y});
}
}
}
}
int ans = 0;
int main() {
scanf("%d%d%d", &n, &m, &X);
int u, v, w;
memset(dis1, INF, sizeof(dis1));
memset(dis2, INF, sizeof(dis2));
for(int i = 1; i <= m; i++) {
scanf("%d%d%d", &u, &v, &w);
add_edge1(u, v, w);
add_edge2(v, u, w);
}
dijkstra1();
dijkstra2();
for(int i = 1; i <= n; i++) {
if(i == X) continue;
if(ans < dis1[i] + dis2[i]) ans = dis1[i] + dis2[i];
}
printf("%d\n", ans);
return 0;
}
洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party的更多相关文章
- 洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party
P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...
- 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party 题解
P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...
- 洛谷P1821 [USACO07FEB]银牛派对Silver Cow Party
题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...
- 洛谷 1821 [USACO07FEB]银牛派对Silver Cow Party
[题解] 其实解法 #include<cstdio> #include<cstring> #include<algorithm> #define LL long l ...
- P1821 [USACO07FEB]银牛派对Silver Cow Party
题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...
- luogu P1821 [USACO07FEB]银牛派对Silver Cow Party
题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...
- 【luogu P1821 [USACO07FEB]银牛派对Silver Cow Party】 题解
题目链接:https://www.luogu.org/problemnew/show/P1821 反向多存一个图,暴力跑两遍 #include <cstdio> #include < ...
- [USACO07FEB]银牛派对Silver Cow Party
题目简叙: 寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100). 每头牛参加 ...
- 「Luogu 1821」[USACO07FEB]银牛派对Silver Cow Party
更好的阅读体验 Portal Portal1: Luogu Portal2: POJ Description One cow from each of N farms \((1 \le N \le 1 ...
随机推荐
- bzoj 1119 [POI2009] SLO & bzoj 1697 牛排序 —— 置换+贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1119 https://www.lydsy.com/JudgeOnline/problem.p ...
- WPF DatePicker 的textbox的焦点
要得到DatePicker的textchange属性, 必须通过TextBoxBase.TextChanged 事件来处理. 想要判断是否当前DatePicker的textbox获取到焦点, 可以通过 ...
- MQTT协议简介及协议原理
MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的“轻量级”通讯协议,该协议构建 ...
- docker 学习(七) docker 容器挂载
1:docker的默认存放位置: $ sudo su # cd /var/lib/docker # ls -F containers/ graph/ repositories volumes/ ...
- 4-数组、指针与字符串1.3-this指针
this指针时一个隐含于每一个类的成员函数中的特殊指针(包括构造函数和析构函数),它用于指向正在被成员函数操作的对象. this指针明确地指出了成员函数当前所操作的数据所属的对象.实际过程是,当通过一 ...
- HDU - 6383 百度之星2018初赛B 1004 p1m2(二分答案)
p1m2 Accepts: 1003 Submissions: 4595 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072 ...
- 我们团队是如何落地DDD的(1)
最近发现文章老是被窃取,有些平台举报了还没有用.请识别我的id方丈的寺院. 摘要 DDD领域驱动设计,起源于2004年著名建模专家Eric Evans发表的他最具影响力的著名书籍:Domain-Dri ...
- xrange与range之间的区别
对于这两个好像功能都差不多,这两个经常会被搞混,所以今天一定要把这个完全弄清楚. 首先我们看看range: range([start,] stop[, step]),根据start与stop指定的范围 ...
- 利用 Docker 包 Laradock 服务器部署 Laravel & ThinkSNS+ 等程序实战(多项目)
什么是ThinkSNS+ ThinkSNS(简称TS),一款全平台综合性社交系统,为国内外大中小企业和创业者提供社会化软件研发及技术解决方案,目前最新版本为ThinkSNS+.ThinkSNS V4. ...
- hp惠普工作站 安装ESXI 从U盘启动黑屏问题
[背景] 公司新买了一套hp z238 workstation需要安装esxi,我按照以前的方法制作好U盘后插入从U盘引导,显示屏一直黑屏,这个问题困扰了我一天....在网上找了好久无果 第二天终于在 ...