poj3268(单源最短路)
来源:互联网 发布:java 如何做物联网开发 编辑:程序博客网 时间:2024/06/11 03:50
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 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3
Sample Output
10
三种最短路算法复杂度一比较就知道应该选用dijkstra算法,而由于边是单向的,所以返回路径要单独算,再算一次dijkstra,这是这次把边的起点终点换一下,解决。
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;#define INF 1e9+1#define MAX_E 100005#define MAX_V 1005int cost[MAX_V][MAX_V];int d[MAX_V];int d2[MAX_V];bool used[MAX_V];int V,M,X;void init(){for(int i=0;i<MAX_V;i++){for(int j=0;j<MAX_V;j++){cost[i][j]=INF;}cost[i][i]=0;}}void dijkstra(int s){fill(d,d+V,INF);fill(used,used+V,false);d[s]=0;while(1){int v=-1;for(int u=0;u<V;u++){if(!used[u]&&(v==-1||d[u]<d[v]))v=u;}if(v==-1)break;used[v]=true;for(int u=0;u<V;u++){d[u]=min(d[u],d[v]+cost[v][u]);}}}void Fake_dijkstra(int s){fill(d2,d2+V,INF);fill(used,used+V,false);d2[s]=0;while(1){int v=-1;for(int u=0;u<V;u++){if(!used[u]&&(v==-1||d2[u]<d2[v]))v=u;}if(v==-1)break;used[v]=true;for(int u=0;u<V;u++){d2[u]=min(d2[u],d2[v]+cost[u][v]); //将边的起点与终点交换}}}int main(){init();cin>>V>>M>>X;int a,b,t;for(int i=0;i<M;i++){cin>>a>>b>>t;cost[a-1][b-1]=t;}dijkstra(X-1);Fake_dijkstra(X-1);int maxt=0;for(int i=0;i<V;i++)if(d[i]+d2[i]>maxt)maxt=d[i]+d2[i];cout<<maxt<<endl;}
- poj3268(单源最短路)
- poj3268(单源最短路,dijkstra算法)
- poj3268(最短路,dijkstra)
- POJ3268之最短路
- poj3268最短路Dijkstrra
- 最短路 dijkstra---poj3268
- poj3268 最短路
- POJ3268【最短路】
- POJ3268 最短路
- 最短路(SPFA+负权回路的判断)-poj3268
- POJ3268—Silver Cow Party(反向最短路)
- POJ3268最短路水题
- 基础最短路四 POJ3268
- poj3268——Silver Cow Party(最短路+godv之力)
- POJ3268————Silver Cow Party(最短路)
- POJ3268 Silver Cow Party DIJ最短路
- POJ3268 Silver Cow Party 最短路
- 【POJ3268】Silver Cow Party 最短路
- Jquery加载完成后弹出层
- C++关系运算和逻辑运算
- redis服务器允许远程主机访问
- 英特尔的安腾 (Itanium) 架构处理器是怎么失败的?
- Makefile学习(1)
- poj3268(单源最短路)
- Zeppelin源码分析-Interpreter 相关类(1)
- 淘淘摘苹果
- 方法的重载
- 基于Servlet&Jsp的网上书店设计(三)
- DOM节点删除之empty和remove区别
- 什么是RESTful?
- 机器学习之模型预估与选择
- 菜鸟与 cef 的邂逅之旅(三):Cef3 中 C++ 与 JavaScript 的互相调用