POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
#include<vector>
#include<iostream>
using namespace std;
const int maxn = 1e4+200;
const int maxo = 1e5+200;
const int INF = 0x3f3f3f3f;
struct Oper{
int u,v,w;
}opers[maxo];
struct HeapNode{
int d;
int u;
bool operator < (const HeapNode & rhs)const {
return d > rhs.d; //
}
};
struct Edge{
int from,to;
int dist;
};
vector<Edge>edge;
vector<int>G[maxn];
priority_queue<HeapNode>PQ;
int d[maxn] , vis[maxn];
int ans[maxn];
int n,m;
void init(){
for(int i = 0; i <= n;i++){
G[i].clear();
}
edge.clear();
}
void AddEdge(int u,int v,int w){
edge.push_back( (Edge){ u ,v, w } );
m = edge.size();
G[u].push_back(m-1);
}
void Dijstra(int s){
for(int i = 0; i <= n; i++){
d[i] = INF;
}
d[s] = 0;
PQ.push( (HeapNode){ d[s],s} );
memset(vis,0,sizeof(vis));
while(!PQ.empty()){
HeapNode x = PQ.top();
PQ.pop();
int u = x.u;
if(vis[u]) continue;
vis[u] = 1;
for( int i = 0; i < G[u].size(); i++){
Edge & e = edge[G[u][i]];
if(d[e.to] > d[e.from]+e.dist){
d[e.to] = d[e.from] + e.dist;
PQ.push( (HeapNode){d[e.to] , e.to} );
}
}
}
}
int main(){
int k , mm;
while(scanf("%d%d%d",&n,&mm,&k)!=EOF){
init();
memset(ans,0,sizeof(ans));
int a,b,c;
for(int i = 0; i < mm; i++){
scanf("%d%d%d",&a,&b,&c);
a--,b--;
AddEdge(b,a,c);
opers[i].u = a ;
opers[i].v = b ;
opers[i].w = c ;
}
Dijstra(k-1);
for(int i = 0; i < n;i++){
ans[i] = d[i];
}
init();
for(int i = 0; i < mm; i++){
AddEdge(opers[i].u,opers[i].v,opers[i].w);
}
Dijstra(k-1);
int res = 0;
for(int i = 0; i < n; i++){
ans[i] += d[i];
if(res < ans[i]){
res = ans[i];
}
}
printf("%d\n",res);
}
return 0;
}
POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】的更多相关文章
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- poj 3268 Silver Cow Party(最短路dijkstra)
描述: One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the bi ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- Poj 3268 Silver cow party 迪杰斯特拉+反向矩阵
Silver cow party 迪杰斯特拉+反向 题意 有n个农场,编号1到n,每个农场都有一头牛.他们想要举行一个party,其他牛到要一个定好的农场中去.每个农场之间有路相连,但是这个路是单向的 ...
- poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13611 Accepted: 6138 ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ-1122 FDNY to the Rescue!---Dijkstra+反向建图
题目链接: https://vjudge.net/problem/POJ-1122 题目大意: 给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路 ...
- POJ 3268 Silver Cow Party (最短路dijkstra)
Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
随机推荐
- Servlet编程实例 续1
-----------------siwuxie095 在 LoginServlet 中,右键->Open Type Hierar ...
- 太有用了,所以转:Delphi下16进制位图数据转位图
如果我们在Form中拖入一个Image控件,并设置好picture后,Alt+F12就可以看到Form的源代码中已经将图片转成了16进制字符串,如下: object Image1: TImage Le ...
- 3.Windows应急响应:蠕虫病毒
0x00 前言 蠕虫病毒是一种十分古老的计算机病毒,它是一种自包含的程序(或是一套程序),通常通过网络途径传播, 每入侵到一台新的计算机,它就在这台计算机上复制自己,并自动执行它自身的程序.常见的蠕虫 ...
- Linus与陈庆
Linus 1969年末,李纳斯出生于芬兰的赫尔辛基市,算是赶上了60后的尾巴.小时候他是个其貌不扬的孩子,除了一个鼻子长的「富丽堂皇」之外乏善可陈.他为了让鼻子看上去小一些,经常戴上眼镜就不愿意摘下 ...
- redis系列:通过通讯录案例学习hash命令
前言 这一篇文章将讲述Redis中的hash类型命令,同样也是通过demo来讲述,其他部分这里就不在赘述了. 项目Github地址:https://github.com/rainbowda/learn ...
- 网络应用(4):块的概念 | Range
分块来处理,也算是自然的想法,就是化整为零.而于对于文件的下载同样使用这个道理,既可整体下载,也可分块下载. 小程这里以http协议为例,来看一下块的概念与使用. http的range http1.0 ...
- 给 asp.net core 写一个简单的健康检查
给 asp.net core 写一个简单的健康检查 Intro 健康检查可以帮助我们知道应用的当前状态是不是处于良好状态,现在无论是 docker 还是 k8s 还是现在大多数的服务注册发现大多都提供 ...
- VIM命令图---可定制版
近期闲来无事,便自己用PS制作了一份VIM命令助记图,之前看到网上有类似的图片,可是有些解释感觉不太好,并有一些错误的地方,所以就自己做了一份,并且保留了PS源文件,为了方便会PS的网友可以对本图进行 ...
- GXU - 7D - 区间求和 - 前缀和
https://oj.gxu.edu.cn/contest/7/problem/D 描述 有一个所有元素皆为0的数组A,有两种操作: 1 l r x表示将A区间[l,r]内所有数加上x: 2 l r表 ...
- 2010辽宁省赛F(字典树,动态规划)
#include<bits/stdc++.h>using namespace std;int n,x;char s[10010];char a[31010];int val[100010] ...