POJ3268 Silver Cow Party (建反图跑两遍Dij)
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?
Input
Lines 2..
M+1: Line
i+1 describes road
i with three space-separated integers:
Ai,
Bi, and
Ti. The described road runs from farm
Ai to farm
Bi, requiring
Ti time units to traverse.
Output
Sample Input
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
Sample Output
10
Hint
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int N=,M=;
int head1[N],ver1[M],edge1[M],Next1[M],head2[N],ver2[M],edge2[M],Next2[M],d1[N],d2[N];//建一个原图,一个反图
bool v1[N],v2[N];
int n,m,tot1=,tot2=,p;
priority_queue<pair<int,int> >q1;
priority_queue<pair<int,int> >q2;
void add1(int x,int y,int z)
{
ver1[++tot1]=y;
edge1[tot1]=z;
Next1[tot1]=head1[x];
head1[x]=tot1;
}
void add2(int x,int y,int z)
{
ver2[++tot2]=y;
edge2[tot2]=z;
Next2[tot2]=head2[x];
head2[x]=tot2;
}
void dijkstra1()
{
memset(d1,0x3f,sizeof(d1));//d数组的初始化一定要根据题意,有时要初始化为0,有时初始化为正无穷(最短路)有时初始化为负无穷( dij或者floyd变式求某条最长边)
memset(v1,,sizeof(v1));
d1[p]=;
q1.push(make_pair(,p));
while(q1.size())
{
int x=q1.top().second;q1.pop();
if(v1[x])continue;
v1[x]=;
int i;
for(i=head1[x];i;i=Next1[i])
{
int y=ver1[i],z=edge1[i];
if(d1[y]>d1[x]+z)
{
d1[y]=d1[x]+z;
q1.push(make_pair(-d1[y],y));
}
}
}
}
void dijkstra2()
{
memset(d2,0x3f,sizeof(d2));
memset(v2,,sizeof(v2));
d2[p]=;
q2.push(make_pair(,p));
while(q2.size())
{
int x=q2.top().second;q2.pop();
if(v2[x])continue;
v2[x]=;
int i;
for(i=head2[x];i;i=Next2[i])
{
int y=ver2[i],z=edge2[i];
if(d2[y]>d2[x]+z)
{
d2[y]=d2[x]+z;
q2.push(make_pair(-d2[y],y));
}
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&p);
int i;
for(i=;i<=m;i++)
{
int t1,t2,t3;
scanf("%d%d%d",&t1,&t2,&t3);
add2(t2,t1,t3);
add1(t1,t2,t3);
}
dijkstra1();
dijkstra2();
int ans=;
for(i=;i<=n;i++)
{
if(i!=p)ans=max(ans,d1[i]+d2[i]);
}
cout<<ans;
return ;
}
POJ3268 Silver Cow Party (建反图跑两遍Dij)的更多相关文章
- 洛谷P1073最优贸易(跑两遍dij)
题目描述 CC C国有n n n个大城市和m mm 条道路,每条道路连接这 nnn个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 mmm 条道路中有一部分为单向通行的道路,一部分为 ...
- LuoguP1342请柬 【最短路/建反图】By cellur925
题目传送门 开始就想直接正向跑一遍Dij把到各点的最短路加起来即可,后来发现与样例少了些,于是再读题发现需要也求出学生们回来的最短路. 但是注意到本题是有向图,如果是无向图就好说. 那么我们怎么解决? ...
- 炸弹:线段树优化建边+tarjan缩点+建反边+跑拓扑
这道题我做了有半个月了...终于A了... 有图为证 一句话题解:二分LR线段树优化建边+tarjan缩点+建反边+跑拓扑统计答案 首先我们根据题意,判断出来要炸弹可以连着炸,就是这个炸弹能炸到的可以 ...
- Luogu P1073 最优贸易【最短路/建反图】 By cellur925
题目传送门 这么经典的题目,还是看了lyd的题解....唉难过. 一句话题意:在一张点有全都的图上找一条从1到n的路径,存在两个点p,q(p<q),使val[q]-val[p]最大. 给出的图是 ...
- Magic Potion(最大流,跑两遍网络流或者加一个中转点)
Magic Potion http://codeforces.com/gym/101981/attachments/download/7891/20182019-acmicpc-asia-nanjin ...
- POJ3268 Silver Cow Party —— 最短路
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ-3268 Silver Cow Party---正向+反向Dijkstra
题目链接: https://vjudge.net/problem/POJ-3268 题目大意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号X,其他牛要去拜访它并且拜访完之后要返回 ...
- POJ 2438 Children’s Dining (哈密顿图模板题之巧妙建反图 )
题目链接 Description Usually children in kindergarten like to quarrel with each other. This situation an ...
- POJ3268 Silver Cow Party(dijkstra+矩阵转置)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15156 Accepted: 6843 ...
随机推荐
- 启动docker报Failed to start Docker Application Container Engine.解决
[root@docker ~]# systemctl status docker.service● docker.service - Docker Application Container Engi ...
- codeforces div2 603 C. Everyone is a Winner!(二分)
题目链接:https://codeforces.com/contest/1263/problem/C 题意:给你一个数字n,求n/k有多少个不同的数 思路:首先K大于n时,n/k是0.然后k取值在1到 ...
- 静态方法使用synchronized修饰.
package seday10;/** * @author xingsir * 静态方法若使用synchronized修饰,这个方法一定具有同步效果.静态方法上使用的同步监视器对象为这个类的" ...
- AcWing 841. 字符串哈希
//快速判断两次字符串是不是相等 #include<bits/stdc++.h> using namespace std ; typedef unsigned long long ULL; ...
- Django流程-以登录功能为例
Django流程-以登录功能为例 一.注意点 1.新创建的app一定要先去settings.py注册 简写:'app01' 完整:'app01.apps.App01Config' 2.启动Django ...
- linux中卸载mysql以及安装yum
卸载mysql:https://blog.csdn.net/qq_41829904/article/details/92966943 链接2:https://www.cnblogs.com/nickn ...
- GIT分布式代码管理系统
1:GTI介绍及使用 环境搭建: 服务器 IP地址 主机名 角色 Centos7.5 192.168.200.113 gitserver GIT服务器 Centos7.5 192.168.200.11 ...
- ajax请求无法下载文件的原因
原因: Ajax下载文件的这种方式本来就是禁止的.出于安全因素的考虑,javascript是不能够保存文件到本地的, 所以ajax考虑到了这点,只是接受json,text,html,xml格式的返回值 ...
- Azure IoT Hub 十分钟入门系列 (2)- 使用模拟设备发送设备到云(d2c)的消息
本文主要分享一个案例: 10分钟- 使用Python 示例代码和SDK向IoT Hub 发送遥测消息 本文主要有如下内容: 了解C2D/D2C消息: 了解IoT Hub中Device的概念 了解并下载 ...
- shell脚本自学之路
阿里云大学教学https://edu.aliyun.com/course/155/ 运行 chmod +x xx.sh ./xx.sh 基本语法:echo 输出 $赋值 特殊变量: $* 变量的使 ...