8.22最短路径讲解
来源:互联网 发布:图片墙制作软件 编辑:程序博客网 时间:2024/06/08 17:21
今天训练赛做了个最短路径的题,floyd超时,老师说一般给的数据小与100你用floyd,其实想过dij,但是当从各个点(源点)到目标点用dij很麻烦,dij的源点是不变的,这个边怎么班,
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?
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.
4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3
10
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iomanip>
using namespace std;
#define inf 0x3f3f3f3f
int dis1[1005],dis2[1005],edge[1005][1005],v[1005];
int n,m,x;
void dij1()
{
for(int i=1;i<=n;i++)
dis1[i]=edge[x][i];//!!!!!!!!!!!!!!!
memset(v,0,sizeof(v));
dis1[x]=0;
v[x]=1;
//cout<<3<<endl;
for(int i=1;i<=n-1;i++)
{//cout<<4<<endl;
int minn,next;
next=0;
minn=inf;
for(int j=1;j<=n;j++)
{
if(!v[j]&&minn>dis1[j])
{
minn=dis1[j];
next=j;
}
}//cout<<5<<endl;
if(next==0) break;
v[next]=1;//cout<<111<<endl;
for(int j=1;j<=n;j++)
{
if(!v[j]&&dis1[j]>dis1[next]+edge[next][j])
dis1[j]=dis1[next]+edge[next][j];//!!!!!!!!!!!!!!!!!!!
}
}//cout<<6<<endl;
}
void dij2()
{
for(int i=1;i<=n;i++)
dis2[i]=edge[i][x];//!!!!!!!!!!!!!!
memset(v,0,sizeof(v));
dis2[x]=0;
v[x]=1;//cout<<7<<endl;
for(int i=1;i<=n-1;i++)
{//cout<<8<<endl;
int minn,next;
next=0;
minn=inf;
for(int j=1;j<=n;j++)
{
if(!v[j]&&minn>dis2[j])
{
minn=dis2[j];
next=j;
}
}
if(next==0) break;
v[next]=1;//cout<<11<<endl;
for(int j=1;j<=n;j++)
{
if(!v[j]&&dis2[j]>dis2[next]+edge[j][next])//!!!!!!!!!!!!!!!!!!!!
dis2[j]=dis2[next]+edge[j][next];//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}//cout<<9<<endl;
}
//cout<<10<<endl;
//cout<<22<<endl;
}
int main()
{
int i,j,a,b,t,maxn;
scanf("%d%d%d",&n,&m,&x);
memset(edge,inf,sizeof(edge));
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&t);
edge[a][b]=t;
}
//cout<<1<<endl;
dij1();//cout<<2<<endl;
dij2();
maxn=0;
for(i=1;i<=n;i++)
{
if(maxn<dis1[i]+dis2[i])
maxn=dis1[i]+dis2[i];
}
cout<<maxn<<endl;
}
好题,怎么自己就是想不到呢。。。。,
第一个dij1()就是平唱的求源点到其他点的最短路径
dij2()求各个点到终点的最短路径,源点在变化,加的时候换一下坐标,就可以将终点作为原点,才是得到dis2(i)就是i到终点的最短路径
- 8.22最短路径讲解
- 最短路径Floyd算法讲解
- 集训8.19最短路径讲解
- 最短路径之Dijkstra算法思想讲解
- 最短路径之Dijkstra算法详细讲解
- 最短路径之Dijkstra算法详细讲解
- 最短路径之Dijkstra算法详细讲解
- 最短路径之Dijkstra算法详细讲解
- 最短路径之Dijkstra算法详细讲解
- 最短路径之Dijkstra算法详细讲解
- 最短路径之Dijkstra算法详细讲解
- 最短路径之Dijkstra算法详细讲解
- 最短路径之Dijkstra算法详细讲解
- 最短路径之Dijkstra算法详细讲解
- 【最短路径之Dijkstra算法详细讲解 】
- 最短路径之Dijkstra算法详细讲解
- 【转载】最短路径之Dijkstra算法详细讲解
- 最短路径算法
- 图的基本存储的基本方式一
- 数组中出现次数超过一半的数字
- hdu--6153(A Secret)
- [转]写给Krpano小白们的最最最入门级教程(二)
- 【容斥原理】Codeforces547C[Mike and Foam]题解
- 8.22最短路径讲解
- spark(5)--spark模型中FIFO的实现
- hdu 2896 ac自动机
- lintcode——买卖股票的最佳时机
- 2017.08.22 今日头条笔试题
- MyBatis(1)--MyBatis入门示例
- Buggy Robot Gym
- Guava Cache源码解析
- 智能停车场解决方案