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;
}
最新文章
- springmvc4 mybatis 整合 框架源码 bootstrap
- struts2进阶篇(2)
- [转载]Eclipse提示No java virtual machine
- 从MySQL 5.5到5.7看复制的演进
- Windows7给C盘扩容
- WPF程序只运行一个实例
- maven的仓库、生命周期与插件
- WinMain初始化详细过程以及消息循环
- unity脚本的基础语法
- python会什么比c慢
- windows上使用SecureCRT连接linux
- linux系统centOS7下搭建redis集群中ruby版本过低问题的解决方法
- [转] HTML5 FormData 方法介绍以及实现文件上传
- StackExchange.Redis和Log4Net构建日志
- Spring Boot整合MyBatis(使用Spring Tool Suite工具)
- spring中xml配置方式和注解annoation方式(包括@autowired和@resource)的区别
- ps常用快捷键
- 使用nginx反向代理解决前端跨域问题
- Mysql读写分离——主从数据库+Atlas
- 【TOJ 1072】编辑距离(动态规划)
热门文章
- pycharm ubuntu安装
- mongodb循环插入测试数据
- [51nod]多重背包模板
- bos物流面试题
- ps -ef | grep java 输出的具体含义是什么?
- LeetCode第14题:最长公共前缀
- Entity Framework 分页处理
- Kolla Ocata版本安装及镜像制作流程
- ERROR 1010 (HY000): Error dropping database (can&#39;t rmdir &#39;./nsd&#39;, errno: 39)
- PHP 预定义常量(魔术常量)