银牛派对

正向建图+反向建图, 两边跑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. Day06:迭代器,生成器,生成表达式,面向过程编程,包及常用模块

    今日内容:1.迭代器(****)2.生成器(***)3.生成器表达式(*****)4.面向过程编程(*****)5.包的使用(***)6.常用模块    logging (*****)    re ( ...

  2. Divide Two Integers-不用'/' '*' '%'操作实现整数的除法

    题目描述: 不用 '*' '/' 和 '%' 运算实现两个整数的除法 题目来源:http://oj.leetcode.com/problems/divide-two-integers/ 题目分析: 例 ...

  3. emacs for OCaml

    (require 'cl) (require 'package) (add-to-list 'package-archives '("melpa" . "https:// ...

  4. wpf RenderTargetBitmap保存控件为图片时图片尺寸不对的问题

    使用RenderTargetBitmap渲染图片时,必须在需要渲染的控件外层包含一个border控件,不然渲染的图片返回就会出现问题. 如下: <Grid> <Grid.RowDef ...

  5. Hibernate注解详细介绍

    引自http://blog.csdn.net/lin_yongrui/article/details/6855394 声明实体Bean      @Entity   public class Flig ...

  6. /dev/mapper/vg_zjxtest-lv_root 占用到达100%的解决方法

    linux系统下报这个错误是磁盘满了,可以先做以下几个检查: 1.查看下面这个目录看看是否是回收站满了 du -sh ~/.local/share/Trash 2.查看/tmp目录和/var目录是否有 ...

  7. iis部署错误:HTTP 错误 500.21 - Internal Server Error

    将网站发布到IIS,访问发生如下错误: HTTP 错误 500.21 - Internal Server Error处理程序“PageHandlerFactory-Integr”在其模块列表中有一个错 ...

  8. 12. nc/netcat 用法举例

    nc命令用法举例 什么是nc nc是netcat的简写,有着网络界的瑞士军刀美誉.因为它短小精悍.功能实用,被设计为一个简单.可靠的网络工具 nc的作用 (1)实现任意TCP/UDP端口的侦听,nc可 ...

  9. putty连接虚拟机注意事项

    1,虚拟机ssh服务要开 2,虚拟机最好把防火墙关掉 3,虚拟机和主机的IP要在同一网段 4,大哥,putty上面那个才是要连接的远程主机IP啊!下面那个是会话名,写什么都行. 5,可以选择UTF8, ...

  10. Linux做脚本定时任务(定时清理日志)

    无论一些面试问题,还是实际应用,都会用到虚拟机的定时任务.现做定时清理日志日志做一总结. 1.查看/etc/crontab文件. linux 系统则是由 cron (crond) 这个系统服务来控制的 ...