银牛派对

正向建图+反向建图, 两边跑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的更多相关文章

  1. 洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party

    P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  2. 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party 题解

    P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  3. 洛谷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 ...

  4. 洛谷 1821 [USACO07FEB]银牛派对Silver Cow Party

    [题解] 其实解法 #include<cstdio> #include<cstring> #include<algorithm> #define LL long l ...

  5. 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 ...

  6. 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 ...

  7. 【luogu P1821 [USACO07FEB]银牛派对Silver Cow Party】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1821 反向多存一个图,暴力跑两遍 #include <cstdio> #include < ...

  8. [USACO07FEB]银牛派对Silver Cow Party

    题目简叙: 寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100). 每头牛参加 ...

  9. 「Luogu 1821」[USACO07FEB]银牛派对Silver Cow Party

    更好的阅读体验 Portal Portal1: Luogu Portal2: POJ Description One cow from each of N farms \((1 \le N \le 1 ...

随机推荐

  1. [转载]IOCP模型的总结

    原文:IOCP模型的总结 IOCP(I/O Completion Port,I/O完成端口)是性能最好的一种I/O模型.它是应用程序使用线程池处理异步I/O请求的一种机制.在处理多个并发的异步I/O请 ...

  2. 51 nod 1522 上下序列——序列dp

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1522 很好的思想.考虑从小到大一对一对填数,这样也能对它的大小限制 ...

  3. WaitHandle.WaitAll 方法在WPF工程中的应用

    因为WaiAll需要多线程支持, 而WPF是STA模式, 可以通过以下方式实现WaitAll ManualResetEvent[] events:  foreach (ManualResetEvent ...

  4. 利用NDK崩溃日志查找BUG

    转自:http://www.tuicool.com/articles/qQNfUfe 背景介绍 本文主要内容: 利用android的crash log来对c++开发的android应用进行错误定位. ...

  5. c# link 学习网站

    http://www.cnblogs.com/shanyou/p/4353433.html

  6. 远程访问Linux系统桌面

     让Windows可以远程访问Linux系统桌面 http://jingyan.baidu.com/article/d8072ac47b810eec95cefde8.html linux系统下,11款 ...

  7. linux 下定位程序假死

    ps -ef | grep 程序名称 pstack 程序的进程ID

  8. echarts学习的一些笔记

    工具栏组件 Show 是否显示 Feature 具体显示的功能 saveAslmage  保存图片 Restore 还原 dataZoom  缩放视图 magicType 动态类型切换 toltip组 ...

  9. 记微软OpenHack机器学习挑战赛

    有幸参加了微软OpenHack挑战赛,虽然题目难度不大,但是很有意思,学到了很多东西,还有幸认识了微软梁健老师,谢谢您的帮助!同时还认识同行的很多朋友,非常高兴,把这段难忘的比赛记录一下~~也分享一下 ...

  10. pyhton 的i/o流和文件操作

    Python 文件I/O 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你传递的表达式转换成一个字符串表达式,并将结果写到标准输出如下: #!/u ...