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 ...
随机推荐
- selenium如何获取已定位元素的属性值?
HTML源代码: <div class="res-status" data-fortune="5" data-selfsos="" d ...
- Learning Python 011 高级特性 2
Python 高级特性 2 列表生成式 列表生成式就是指类似这样的代码:[x for x in range(1, 11)] >>> L = [x for x in range(1, ...
- 33、VCF格式
转载:http://blog.sina.com.cn/s/blog_7110867f0101njf5.html http://www.cnblogs.com/liuhui0622/p/6246111. ...
- windows10 启用Linux子系统
转载 https://jingyan.baidu.com/article/e2284b2b99a327e2e6118d38.html 打开Windows下 设置--更新和安全--针对开发人员--选中“ ...
- python包管理
如果是python 项目目录,例如pycharm里新建的python项目,则可以通过from,import导入目录下的文件夹. 如果是普通文件目录,则代码里不能相对方式导入该目录下的文件夹,需要加入要 ...
- 1.4 DVWA亲测XSS漏洞
首先需要有配置好的DVWA环境,像下图这样 其中: XSS (DOM) : DOM型XSS漏洞 XSS (Reflected) : 反射性XSS漏洞 XSS (Stored) : 存储型XS ...
- URAL 2018 The Debut Album (DP)
题意:给出n长度的数列,其实1的连续个数不超过a,2的连续个数不超过b. 析:dp[i][j][k] 表示前 i 个数,以 j 结尾,并且连续了k个长度,要用滚动数组,要不然MLE. 代码如下: #p ...
- Django 之 auth 模块
Django 内置一个 auth 模块,帮助用户实现注册.登录.注销以及修改密码等功能,帮助开发者省去了很多功夫. auth 模块 在创建模型时,Django内部会生成一个名为 auth_user 的 ...
- IOHelper(自制常用的输入输出的帮助类)
常用的读写文件,和地址转换(win和linux均支持),操作文件再也不是拼接那么的low了 using System; using System.Diagnostics; using System.I ...
- 51nod1305(简单逻辑)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1305 题意:中文题诶- 思路:1e5的数据直接暴力肯定是不行 ...