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 ...
随机推荐
- Java常见设计模式之工厂模式
工厂模式在我们日常的应用中应当算是比较广泛的一种设计模式了.今天让我们一起来学习一下,工厂的设计模式. 工厂模式在<Java与模式>中分为三类: 1)简单工厂模式(Simple F ...
- rails中一个窗体多个模型——fields_for
详细参考 http://railscasts.com/episodes/73-complex-forms-part-1中part-1.2.3部分 借助field_for可以生成表单来处理两个或更多模型 ...
- sql server 表索引碎片处理
DBCC SHOWCONTIG (Transact-SQL) SQL Server 2005 其他版本 更新日期: 2007 年 9 月 15 日 显示指定的表或视图的数据和索引的碎片信息. 重要提示 ...
- [Android Lint] xxx is not translated in xxx 的解决方法
CLEAN项目即可 转自BLOG http://blog.csdn.net/feng88724/article/details/8835664
- 项目积累demo-01
1 搭建Spring-Boot项目 在这里我使用intellij新建spring boot工程: 点击next; 输入Group以及artifact之后.点击next.之后点击web.接着finish ...
- R: data.frame 数据框的:查询位置、排序(sort、order)、筛选满足条件的子集。。
################################################### 问题:数据框 data.frame 查.排序等, 18.4.27 怎么对数据框 data.f ...
- HDU 3966 Aragorn's Story (简单树链剖分)
题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上的所有点权值加上K D C1 C2 K:把C1与C2的路径上的所有点权值减去K Q C:查询节点编号为C ...
- SPI编程
#include <stdio.h>#include <wiringPi.h>#include <wiringPiSPI.h> int main(void){ un ...
- 谈谈Java异常处理这件事儿
此文已由作者谢蕾授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前言 我们对于"异常处理"这个词并不陌生,众多框架和库在异常处理方面都提供了便利,但是对于 ...
- CODING 告诉你硅谷项目经理的项目管理之道(2)
优秀的项目管理者是怎么工作的?如何帮助研发团队高效工作?这一直是 CODING 关注的重要话题,我们不断地打磨 CODING 研发管理系统来让开发更简单. 近期我们精心挑选了几篇硅谷科技公司研发管理者 ...