POJ-3268-最短路(dijkstra算法)
Silver Cow Party
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 12494 | Accepted: 5568 |
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; roadi 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
M, and X
Lines 2..M+1: Line i+1 describes road i with three space-separated integers:Ai,
Bi, and Ti. The described road runs from farmAi 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
Source
field=source&key=USACO+2007+February+Silver">USACO 2007 February Silver
题目的意思是:从出发点到达目的地X。再从X返回到出发点的最短路径中的最大值(由于出发点没有固定,也就是能够:1->X->1, 2->X->2等)。
所以我们要枚举全部的可能性。找出当中的最大值。
巧妙地运用dijkstra算法,双向求出两次X->m的最短路径长然后相加即得到了m->X->m的最短路径。代码例如以下:
#include<queue>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct Edge
{
int to;
int dis;
Edge(int to, int dis){
this -> to = to;
this -> dis = dis;
}
};
typedef pair<int,int>P; int a,b,c;
int N,M,X;
int d1[1005],d2[1005];
vector<Edge> G1[1005];
vector<Edge> G2[1005];
void dijkstra(int s,int d[],vector<Edge> G[])
{
priority_queue<P,vector<P>,greater<P> >q;
d[s]=0;
q.push(P(0,s));
while(q.size())
{
P p=q.top();
q.pop();
int v=p.second;
for(int i=0;i<G[v].size();i++)
{
Edge& e=G[v][i];
if(d[e.to]>d[v]+e.dis)
{
d[e.to]=d[v]+e.dis;
q.push(P(d[e.to],e.to));
}
}
}
}
int main()
{
memset(d1,0x5f,sizeof(d1));
memset(d2,0x5f,sizeof(d2));
scanf("%d%d%d",&N,&M,&X);
for(int i=1;i<=M;i++)
{
scanf("%d%d%d",&a,&b,&c);
G1[a].push_back(Edge(b,c));
G2[b].push_back(Edge(a,c));
}
dijkstra(X,d1,G1);
dijkstra(X,d2,G2);
int small_max=-1;
for(int i=1;i<=N;i++)
{
if(i==X) continue;
small_max=max(small_max,d1[i]+d2[i]);
}
cout<<small_max<<endl;
}
POJ-3268-最短路(dijkstra算法)的更多相关文章
- poj 3268 最短路dijkstra *
题目大意:给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都有一个参加聚会并且回来的最 ...
- 单源最短路dijkstra算法&&优化史
一下午都在学最短路dijkstra算法,总算是优化到了我能达到的水平的最快水准,然后列举一下我的优化历史,顺便总结总结 最朴素算法: 邻接矩阵存边+贪心||dp思想,几乎纯暴力,luoguTLE+ML ...
- 最短路Dijkstra算法的一些扩展问题
最短路Dijkstra算法的一些扩展问题 很早以前写过关于A*求k短路的文章,那时候还不明白为什么还可以把所有点重复的放入堆中,只知道那样求出来的就是对的.知其然不知其所以然是件容易引发伤痛的 ...
- 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算法)
Silver Cow Party Descriptions 给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x, ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- 单源最短路Dijkstra算法——matlab实现
迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他节点的最短路径. 它的主要特点是以起始点为中心向外层层扩展(广度优先搜索思想),直到扩展到终点为止. 基本思想 通过Dijk ...
- hdu2544 最短路 Dijkstra算法
最短路(Dijkstra算法模板题) Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 单源最短路(Dijkstra算法)
#返回上一级 @Author: 张海拔 @Update: 2015-03-11 @Link: http://www.cnblogs.com/zhanghaiba/p/3514570.html Dijk ...
- 单源最短路——dijkstra算法
Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止. 问 ...
随机推荐
- js操作元素透明度以及浏览器兼容性
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- python自动化--mock、webservice及webdriver模拟手机浏览器
一.mock实现 自定义一个类,用来模拟未完成部分的开发代码 class Say(): def say_hello(self): pass 自定义返回值 import unittest from un ...
- JavaScript设计模式 (1) 原型模式
原型模式(Prototype):用原型实例指向创建类对象,使用于创建新对象的类共享原型对象的属性以及方法. //图片轮播类 var LoopImages = function (imgArr, con ...
- HDU_3182_Hamburger Magi_状态压缩dp
Hamburger Magi Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Vue课程思维导图
- 虚拟机找不到本机vmnet0,vmnet8,无法连接xshell,解决方案
首先出现这个问题肯定是第一次下载虚拟机把之前的注册表覆盖了,网卡找不到,首先卸载VMware 并且将C\ProgramData下的VMware文件夹删除掉 ,下载cceaner,点击注册表清除干净,再 ...
- maven入门链接
http://www.cnblogs.com/now-fighting/p/4857625.html
- vue 使用vue-i18n做全局中英文切换
1.vue-i18n安装 npm install vue-i18n --save-dev 2.在main.js文件中引入 import VueI18n from 'vue-i18n'; Vue.use ...
- Effective C++标题整理
Effective C++ 话说光看这50个tip又有什么用呢?只有实际使用的时候才体现它们的价值才对. 就像只看<代码大全>不能成为一个好程序员,必须结合实际写项目经验才行. 从C转向C ...
- NOIP2016 DAY1 T2天天爱跑步
传送门 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.«天天爱跑步»是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一一棵包含 ...