P1821 [USACO07FEB]银牛派对Silver Cow Party

题目描述

One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

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

每头牛参加完派对后都必须回家,无论是去参加派对还是回家,每头牛都会选择最短路径,求这N头牛的最短路径(一个来回)中最长的一条路径长度。

输入格式

第一行三个整数N,M, X;

第二行到第M+1行:每行有三个整数Ai,Bi, Ti ,表示有一条从Ai农场到Bi农场的道路,长度为Ti。

输出格式

一个整数,表示最长的最短路得长度。

输入输出样例

输入 #1

4 8 2

1 2 4

1 3 2

1 4 7

2 1 1

2 3 5

3 1 2

3 4 4

4 2 3

输出 #1

10

说明/提示

SPFA

【注意注意】

这是单向路单向路单向路!!!

重要的事情说三遍!!!

(题目中没点出这点但是我的全WA经历让我深刻的认识到了这一点)

【思路】

去和来这是两个完全相反的东西

SPFA跑一个方向是很轻松的

然后另一个方向就很难办了

该怎么办呢?

n遍SPFA?

不太可能这就是一道黄题

对了!可以反向建图!

将方向反过来建出来的图就是完全相反的

某个点到x的距离恰好就是回家的最短距离

这样和正向建图一结合

就能求出去和回的最短路径了

然后比较最大的和

输出就好了

【完整代码】

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int Max = 100005;
struct node
{
int y;
int ne;
int z;
}a1[Max << 1],a2[Max << 1];
int n,m,x;
const int M = 1002;
int head1[M],head2[M];
int sum = 0;
void add1(int x,int y,int z)
{
a1[++ sum].y = y;
a1[sum].z = z;
a1[sum].ne = head1[x];
head1[x] = sum;
}
void add2(int x,int y,int z)
{
a2[++ sum].y = y;
a2[sum].z = z;
a2[sum].ne = head2[x];
head2[x] = sum;
} int d1[M],d2[M];
bool use[M];
void SPFA1()
{
memset(use,false,sizeof(use));
queue<int>q;
for(register int i = 1;i <= n;++ i)
d1[i] = 999999;
d1[x] = 0;
q.push(x);
while(!q.empty())
{
int qwq = q.front();
q.pop();use[qwq] = false;
for(register int i = head1[qwq];i != 0;i = a1[i].ne)
{
int awa = a1[i].y;
if(d1[awa] > d1[qwq] + a1[i].z)
{
d1[awa] = d1[qwq] + a1[i].z;
if(use[awa] == false)
{
use[awa] = true;
q.push(awa);
}
}
}
}
}
void SPFA2()
{
memset(use,false,sizeof(use));
queue<int>q;
for(register int i = 1;i <= n;++ i)
d2[i] = 999999;
d2[x] = 0;
q.push(x);
while(!q.empty())
{
int qwq = q.front();
q.pop();use[qwq] = false;
for(register int i = head2[qwq];i != 0;i = a2[i].ne)
{
int awa = a2[i].y;
if(d2[awa] > d2[qwq] + a2[i].z)
{
d2[awa] = d2[qwq] + a2[i].z;
if(use[awa] == false)
{
use[awa] = true;
q.push(awa);
}
}
}
}
} int main()
{
scanf("%d%d%d",&n,&m,&x);
int xx,yy,zz;
for(register int i = 1;i <= m;++ i)
{
scanf("%d%d%d",&xx,&yy,&zz);
add1(xx,yy,zz);
add2(yy,xx,zz);
}
SPFA1();
SPFA2();
int MM = 0;
for(register int i = 1;i <= n;++ i)
MM = max(MM,d1[i] + d2[i]);
cout << MM << endl;
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

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

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

    银牛派对 正向建图+反向建图, 两边跑dijkstra,然后将结果相加即可. 反向建图以及双向建图的做法是学习图论的必备思想. #include <iostream> #include & ...

  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. linux下nginx部署以及配置详解

    1.下载源码包解压编译 启动多个,请看:在linux系统下安装两个nginx以及启动 查看nginx包路径:http://nginx.org/download/,两种下载方式: 1.在官网下载使用Xf ...

  2. java之hibernate之加载策略和抓取策略

    1.加载策略:指hibernate查询数据时,采用什么样的方式将数据写入内存.Hibernate中提供了两种方式来加载数据:懒加载和即时加载. 2.懒加载又称延迟加载,指使用hiberante API ...

  3. C# 简单的定时器使用

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  4. 1.Tomcat组件梳理—Bootstrap启动器

    Tomcat组件梳理-Bootstrap启动器 一开始是直接从Server开始做梳理的,但是发现有很多东西是从Catalina传输过来的,Catalina又是从Bootstrap启动的,所以还是回过头 ...

  5. python 基础(文件)

    文件句柄:可简单理解为应该内存对象 open()函数  参考 https://www.runoob.com/python3/python3-file-methods.html 读.写.追加 ''' t ...

  6. 2019 字节跳动java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.字节跳动等公司offer,岗位是Java后端开发,因为发展原因最终选择去了字节跳动,入职一年时间了,也成为了面 ...

  7. 关于justify-content属性的再学习(区分三个属性)

    justify-content属性: 用来表示可伸缩项目在主轴方向上的对齐方式: 取值范围为flex-start,flex-end,center,space-between,space-around: ...

  8. 【转载】华为荣耀V9手机如何设置WiFi热点共享

    有时候我们在电脑的时候发现没有无线网络以及有线网络,如果你的手机有相应网络,并且流量足够(当前很多手机流量套餐都是不限量了),可以开启手机上的Wifi热点进行流量共享使用,开启Wifi流量热点后,电脑 ...

  9. vue+element 通过ref修改一切硬核样式~

    今天的需求是这样的,点击按钮,弹出一个Popover 弹出框 然后老大说,把弹出框往下移移,box-shadow值设的大一些... 然后就查看elenent的Popover文档,并没有方法,而且这个组 ...

  10. MySQL用户与权限

    用户连接到mysql,并做各种查询,在用户和服务器中间分为两个阶段: 1:用户是否有权连接上来 2:用户是否有权执行此操作(如select,update等等) 先看第一个阶段:服务器如何判断用户是否有 ...