Floyd(多源最短路径)(O(n^3))
练习
A - 最短路 HDU - 2544 (模板)
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。 输入保证至少存在1条商店到赛场的路线。 Output 对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间 Sample Input 2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0 Sample Output 3 2 AC代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int MAX=0x3f3f3f3f;
int n,m,a,b,c;
int dis[110][110];
void floyd()
{
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
int main()
{
while(cin>>n>>m&&(m+n))
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) dis[i][j]=0;
else dis[i][j]=MAX;
}
}
while(m--)
{
cin>>a>>b>>c;
dis[a][b]=min(dis[a][b],c);
dis[b][a]=dis[b][a];
}
floyd();
cout<<dis[1][n]<<endl;
}
return 0;
}
F - Cow Contest POJ - 3660 (微变形)
N (1 ≤ N ≤ 100) cows, conveniently numbered 1…N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors. The contest is conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B), then cow A will always beat cow B. Farmer John is trying to rank the cows by skill level. Given a list the results of M (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory. Input
- Line 1: Two space-separated integers: N and M
- Lines 2…M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B
Output - Line 1: A single integer representing the number of cows whose ranks can be determined
Sample Input 5 5 4 3 4 2 3 2 1 2 2 5 Sample Output 2 AC代码:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int n,m;
bool arr[110][110];
int cnt[110],ans;
void floyd()
{
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
arr[i][j]=arr[i][j]||(arr[i][k]&&arr[k][j]);
}
}
}
int main()
{
ans=0;
memset(arr,0,sizeof(arr));
memset(cnt,0,sizeof(cnt));
cin>>n>>m;
while(m--)
{
int a,b;
cin>>a>>b;
arr[a][b]=1;
}
floyd();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if((arr[i][j]||arr[j][i])&&i!=j)
cnt[i]++;
}
if(cnt[i]==n-1) ans++;
}
cout<<ans<<endl;
return 0;
}
C - 一个人的旅行 HDU - 2066 (Floyd+剪枝)
虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,0),很多事,还能丰富自己的阅历,还可以看美丽的风景……草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女……眼看寒假就快到了,这么一大段时间,可不能浪费啊,一定要给自己好好的放个假,可是也不能荒废了训练啊,所以草儿决定在要在最短的时间去一个自己想去的地方!因为草儿的家在一个小镇上,没有火车经过,所以她只能去邻近的城市坐火车(好可怜啊~)。 Input 输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个,草儿想去的地方有D个; 接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time小时;(1=<(a,b)<=1000;a,b 之间可能有多条路) 接着的第T+1行有S个数,表示和草儿家相连的城市; 接着的第T+2行有D个数,表示草儿想去地方。 Output 输出草儿能去某个喜欢的城市的最短时间。 Sample Input 6 2 3 1 3 5 1 4 7 2 8 12 3 8 4 4 9 12 9 10 2 1 2 8 9 10 Sample Output 9 AC代码: 多源最短路径,这里城市标号范围到1000,Floyd时间复杂度n^3,不剪枝的话会TLE。 还有个点是Floyd里面三重循环的那个ijk不要轻易动,除了剪枝还是让它去遍历每一个点。(wa了无数发后学的教训呜呜呜) 我居然改了之后还沾沾自喜想自己真聪明别人肯定都想不到呜呜呜天呐怎么会有我这么蠢的人
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
const int MAX=0x3f3f3f3f;
int t,s,d,a,b,n,time,ans;
int dis[1100][1100];
int cs[1100];
void floyd()
{
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
if(dis[i][k]==MAX) continue;
for(int j=1;j<=n;j++)
{
if(dis[k][j]==MAX) continue;
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
}
int main()
{
while(scanf("%d%d%d",&t,&s,&d)!=EOF)
{
memset(cs,0,sizeof(cs));
for(int i=1;i<1100;i++)
{
for(int j=1;j<1100;j++)
{
if(i==j) dis[i][j]=0;
else dis[i][j]=MAX;
}
}
n=0,ans=MAX;
while(t--)
{
scanf("%d%d%d",&a,&b,&time);
dis[a][b]=min(dis[a][b],time);
dis[b][a]=dis[a][b];
n=max(n,max(a,b));
}
for(int i=1;i<=s+d;i++) scanf("%d",&cs[i]);
floyd();
for(int i=1;i<=s;i++)
{
for(int j=s+1;j<=s+d;j++)
ans=min(dis[cs[i]][cs[j]],ans);
}
printf("%d\n",ans);
}
return 0;
}
Dijkstra算法朴素版 (单源最短路径)(O(n^2))
练习
I - Til the Cows Come Home POJ - 2387 (模板)
兔子很热爱学习,为了多学习她希望可以找最快的路线去上学。她的城市里有N个(2 <= N <= 1000)个地铁站,编号分别为1…N。兔子家在1号地铁站旁边,学校是N号地铁站。地铁站之间共有M (1 <= M <= 2000)条双向路径。兔子现在在1号地铁站,她希望知道到学校最短需要多少时间。可以保证兔子能到达学校。忽略换乘地铁的等待时间 Input 第一行:m和n 接下来的m行:每一行有x,y,z三个整数。代表x站到y站或者y站到x站需要z分钟(1<=z<=100) Output 兔子到学校最少需要多少时间。 Sample Input 5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100 Sample Output 90 AC代码: 这道题N到1000,直接Floyd肯定超时,感觉与c题差不多,剪枝应该能过,既然这样…就用dijkstra来写⑧🤪🤪
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
const int MAX=0x3f3f3f3f;
int t,n,a,b,x;
int dis[1010],dq[1010][1010];
bool vis[1010];
void dijkstra()
{
dis[1]=0;
vis[1]=1;
for(int i=1;i<=n;i++)
{
int minn=MAX;
int t=0;
for(int j=1;j<=n;j++)
{
if(vis[j]==0&&minn>dis[j])
{
minn=dis[j];
t=j;
}
}
vis[t]=1;
for(int j=1;j<=n;j++)
{
if(vis[j]==0)
dis[j]=min(dis[j],dis[t]+dq[t][j]);
}
}
}
int main()
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
dis[i]=MAX;
for(int j=1;j<=n;j++)
{
if(i==j) dq[i][j]=0;
else dq[i][j]=MAX;
}
}
scanf("%d%d",&t,&n);
while(t--)
{
scanf("%d%d%d",&a,&b,&x);
dq[a][b]=min(dq[a][b],x);
dq[b][a]=dq[a][b];
}
for(int i=1;i<=n;i++)
dis[i]=dq[1][i];
dijkstra();
printf("%d\n",dis[n]);
return 0;
}
D - Choose the best route HDU - 2680 (记录一个奇奇怪怪的wa点)
One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n. Input There are several test cases. Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home. Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes . Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations. Output The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”. Sample Input 5 8 5 1 2 2 1 5 3 1 3 4 2 4 7 2 5 6 2 3 5 3 5 1 4 5 1 2 2 3 4 3 4 1 2 3 1 3 4 2 3 2 1 1 Sample Output 1 -1 AC代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
const int N=1100;
const int MAX=0x3f3f3f3f;
int n,m,s,p,q,t,w;
int st[N],dis[N],dq[N][N];
bool vis[N];
void dijkstra()
{
vis[s]=1;
dis[s]=0;
for(int i=1;i<=n;i++)
{
int minn=MAX,kk=1;
for(int j=1;j<=n;j++)
{
if(vis[j]==0&&dis[j]<minn)
{
minn=dis[j];
kk=j;
}
}
vis[kk]=1;
for(int j=1;j<=n;j++)
{
if(vis[j]==0)
dis[j]=min(dis[j],dis[kk]+dq[kk][j]);
}
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&s)!=EOF)
{
int ans=MAX;
for(int i=1;i<=n;i++)
{
dis[i]=MAX;
for(int j=1;j<=n;j++)
{
if(i==j) dq[i][j]=0;
else dq[i][j]=MAX;
}
}
memset(st,0,sizeof(st));
memset(vis,0,sizeof(vis));
while(m--)
{
scanf("%d%d%d",&p,&q,&t);
dq[q][p]=min(dq[q][p],t);
}
scanf("%d",&w);
for(int i=0;i<w;i++)
scanf("%d",&st[i]);
for(int i=1;i<=n;i++)
dis[i]=dq[s][i];
dijkstra();
for(int i=0;i<w;i++)
ans=min(ans,dis[st[i]]);
if(ans==MAX) printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}
|