银牛派对

正向建图+反向建图, 两边跑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. TTS API 使用

    #define SPCAT_VOICES           L"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices" ...

  2. 0001_第一个测试小程序Login

    # -*- coding:utf-8 -*- user = raw_input("Username:") password = raw_input("Password:& ...

  3. openstack介绍(二)

    OpenStack services 本节将详细描述OpenStack服务. Compute service overview(计算服务概述) 使用OpenStack云计算计算主机和管理系统.Open ...

  4. prototype for '类名::函数名'does not match any in class'类名'

    函数声明和定义参数类型必须相同. 前置声明一定要放到名称空间内,代表该名称空间内的类.

  5. linux中syslog自定义存储路径的方法

    方法一: 1. su  //切换到root用户下2. cp /etc/sysconfig/rsyslog /etc/sysconfig/rsyslogbak //备份vim /etc/sysconfi ...

  6. 基于Laravel框架的一个简单易学的微信商城(新手必学)

    俗话说,麻雀虽小可五脏俱全呀! 今天分享的这个基于Laravel的小项目大概功能有这些: 1.实现会员登录.注册功能.数据双向验证功能.2.实现手机短信验证.邮件激活账号.邮件通知.3.ajax提交数 ...

  7. UVaLive 3530 Martian Mining (简单DP)

    题意:给定一个n*m的网格,每个格子里有A矿和B矿数量,A必须由右向左运,B只能从下向上运,中间不能间断,问最大总数量. 析:一个简单DP,dp[i][j] 表示 从 (0, 0) 到 (i, j) ...

  8. matlab新手入门(三)(翻译)

    数组索引 MATLAB®中的每个变量都是一个可以容纳多个数字的数组.当您要访问阵列的选定元素时,请使用索引.例如,考虑4乘4A: A = magic(4) A =    16 2 3 13     5 ...

  9. Javascript 获取客户端的运营商 IP 地址 等

    客户端获取运营商 会弹出安全隐患问题,需要修改IE activx 选项, 非常麻烦,用我的代码可以轻松获取. <script src="JS/jquery-1.4.1.js" ...

  10. Spring Data JPA stackoverflow

    1.禁止使用lombok 的@Data 注释 使用@Data注释后,默认会重写父类的toString()方法,hashcode()等方法,在往map里存的时候,会根据equals和hashcode方法 ...