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 ...
随机推荐
- 每天进步一点点------创建Microblaze软核(三)
第七步 进入SDK开发环境编译完成后弹出如下对话框,选择SDK的工作目录.在MicroblazeTutor中创建一个Workspace文件夹,并选择该文件夹为SDK的工作目录.进入SDK主界面.第八步 ...
- 洛谷 P1659 [国家集训队]拉拉队排练(Manacher)
题目链接:https://www.luogu.com.cn/problem/P1659 思路: 首先跑一遍Manacher,用$cnt_i$记录长为$i$的回文串有多少个. 所记录的$cnt$并不是最 ...
- MYSQL导入CSV格式文件数据执行提示错误(ERROR 1290): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement.
MYSQL导入CSV格式文件数据执行提示错误(ERROR 1290): The MySQL server is running with the --secure-file-priv option s ...
- js实现图片轮播图
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- js数组和对象
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HTTP状态码详解(上)
HTTP状态码的英文为 HTTP Status Code.下面是常见的HTTP状态码: 200 - 请求成功 301 - 资源(网页等)被永久转移到其它URL 404 - 请求的资源(网页等)不存在 ...
- 第十一篇 深入Python的dict和set(二)
- Bugku-CTF之江湖魔头(学会如来神掌应该就能打败他了吧)
Day39 江湖魔头 200 http://123.206.31.85:1616/ 学会如来神掌应该就能打败他了吧
- Nuxt项目文件目录结构解释
|-- .nuxt // Nuxt自动生成,临时的用于编辑的文件,build |-- assets // 用于组织未编译的静态资源入LESS.SASS 或 JavaScript |-- compone ...
- 定义列属性:null,default,PK,auto_increment
*定义列属性* 1.列属性NULL: 是否为空,规定一个字段的值是否可以是null: null|not null 2.默认值属性:default value 默认值,在没有为该字段设置值时启用:而且 ...