洛谷 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 ...
随机推荐
- 《TCP/IP详解卷一:协议》数据链路层(一)
版权声明:本文为博主原创文章,转载请标注转载链接,谢谢. 目录(?)[+] 引言 在TCP/IP协议族中,链路层主要有三个目的: 为IP模块发送和接收IP数据报. 为ARP模块 ...
- DCloud-HTML5+:barcode
ylbtech-DCloud:barcode&camera 1.返回顶部 1. http://www.html5plus.org/doc/zh_cn/barcode.html 2. Barco ...
- 一 Kubernetes介绍
Kubenetes是一款由Google开发的开源的容器编排工具,它可以解决以下分布式环境下的问题: 调度 你已经得到了这个很棒的基于容器的应用程序? 太棒了!现在你需要确保它能够运行在它应该运行的地方 ...
- 【VS工程设置】 编译动态库,命令行添加参数,不使用预编译头,指定该项目链接 哪种 运行库
编译动态库 注意: 动态库: [目标文件扩展] => .dll + [配置类型] => 动态库(.dll) 静态库: [目标文件扩展] => .lib + [ 配置类型]=> ...
- httpd服务相关实验
实验环境: CentOS6.8 1.连接测试: 在/etc/httpd/conf/httpd.conf telnet 172.16.252.242 80 GET /index.html HTTP/1. ...
- Advanced R之构造子集
转发请声明出处:http://www.cnblogs.com/lizichao/p/4794733.html 构造子集 R构造子集的操作功能强大而且速度快.精通构造子集者可以用简洁的方式表达复杂的操作 ...
- mvc 自定义 AuthorizeAttribute 验证逻辑
public class AuthorizationFilterAttribute : AuthorizeAttribute { Dictionary<string, string> ro ...
- JMeter聚合报告(Aggregate Report)理解
部分内容转载: http://blog.csdn.net/lion19930924/article/details/51189218 http://www.cnblogs.com/fnng/archi ...
- gin-swagger包Api文档生成, Post请求参数无法接收, 问题修复。
Bug描述 FormData方式下,任意参数类型都只生成file参数类型. 问题重现 问题代码在这一行 github.com\swaggo\swag\operation.go : 131 line c ...
- java面试一定会遇到的56个面试题
1.问题:如果main方法被声明为private会怎样? 答案:能正常编译,但运行的时候会提示”main方法不是public的”. 2.问题:Java里的传引用和传值的区别是什么? 答案:传引用是指传 ...