hdu1874 畅通project续 最短路 floyd或dijkstra或spfa

Problem Description
某省自从实行了非常多年的畅通project计划后。最终修建了非常多路。只是路多了也不好,每次要从一个城镇到还有一个城镇时,都有很多种道路方案能够选择。而某些方案要比还有一些方案行走的距离要短非常多。这让行人非常困扰。

如今,已知起点和终点。请你计算出要从起点到终点,最短须要行走多少距离。

Input
本题目包括多组数据,请处理到文件结束。
每组数据第一行包括两个正整数N和M(0< N<200,0 < M<1000),分别代表现有城镇的数目和已修建的道路的数目。

城镇分别以0~N-1编号。


接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B < N,A!=B,0< X <10000),表示城镇A和城镇B之间有一条长度为X的双向道路。


再接下一行有两个整数S,T(0<=S,T < N),分别代表起点和终点。

Output
对于每组数据,请在一行里输出最短须要行走的距离。假设不存在从S到T的路线,就输出-1.

Sample Input
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2

Sample Output
2
-1

Author
linle

Source
2008浙大研究生复试热身赛(2)——全真模拟

起点到终点的最短路 这里给出3种算法。Floyd。dijkstra和spfa
从提交的代码得速度来看。dijkstra>spfa>Floyd
可是别人都说spfa最快。。。(我就不知道了)
folyd不推荐使用。由于他最慢。,数据大一点的话更不行。

。(只是他最简单)
(1)Floyd

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#pragma comment(linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define EPS 1e-6
#define INF (1<<28)
using namespace std;

int cost[205][205];
bool used[205];
int n,m;
int d[205];
void floyd()
{
    for(int k=0;k<n;k++)
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
}
int main()
{
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        int i,j;
        for(i=0;i<n;i++)
        {
            cost[i][i]=0;
            for(j=0;j<n;j++)
            {
                if(i!=j) cost[i][j]=INF;
            }
        }
        int a,b,value,start,endl;
        for(i=0;i<m;i++)
        {
            scanf("%d %d %d",&a,&b,&value);
            if(cost[a][b]>value) cost[a][b]=cost[b][a]=value;  //多条路的情况。

} scanf("%d %d",&start,&endl); floyd(); if(cost[start][endl]!=INF) printf("%d ",cost[start][endl]); else printf("-1 "); } return 0; }

2:dijkstra

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#pragma comment(linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define EPS 1e-6
#define INF (1<<28)
using namespace std;

int cost[205][205];
bool used[205];
int n,m;
int d[205];
void dijkstra(int s)
{
    fill(d,d+n,INF);
    fill(used,used+n,false);
    d[s]=0;
    while(true)
    {
        int v=-1;
        for(int u=0;u<n;u++)
        {
            if(!used[u]&&(v==-1||d[u]<d[v])) v=u;
        }
        if(v==-1) break;
        used[v]=true;
        for(int u=0;u<n;u++)
        {
            d[u]=min(d[u],d[v]+cost[v][u]);
        }
    }
}
int main()
{
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        int i,j;
        for(i=0;i<n;i++)
        {
            cost[i][i]=0;
            for(j=0;j<n;j++)
            {
                if(i!=j) cost[i][j]=INF;
            }
        }
        int a,b,value,start,endl;
        for(i=0;i<m;i++)
        {
            scanf("%d %d %d",&a,&b,&value);
            if(cost[a][b]>value) cost[a][b]=cost[b][a]=value; //多条路的情况。

} scanf("%d %d",&start,&endl); dijkstra(start); if(d[endl]!=INF) printf("%d ",d[endl]); else printf("-1 "); } return 0; }

3:spfa

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#pragma comment(linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define EPS 1e-6
#define INF (1<<28)
using namespace std;

int cost[205][205];
bool used[205];
int n,m;
int d[205];
void SPFA(int src,int des)
{
    int i;
    for(i=0;i<n;i++) d[i]=INF;
    memset(used,false,sizeof(used));

    queue<int> myqueue;
    while(!myqueue.empty()) myqueue.pop();//清空队列

    d[src]=0;
    used[src]=1;
    myqueue.push(src);
    int tmp;
    while(!myqueue.empty())
    {
        tmp=myqueue.front();
        myqueue.pop();
        used[tmp]=0;
        for(i=0;i<n;i++)
            if(d[i]>d[tmp]+cost[tmp][i])
            {
                d[i]=d[tmp]+cost[tmp][i];
                if(!used[i])
                {
                    used[i]=1;
                    myqueue.push(i);
                }
            }
    }
}

int main()
{
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        int i,j;
        for(i=0;i<n;i++)
        {
            cost[i][i]=0;
            for(j=0;j<n;j++)
            {
                if(i!=j) cost[i][j]=INF;
            }
        }
        int a,b,value,start,endl;
        for(i=0;i<m;i++)
        {
            scanf("%d %d %d",&a,&b,&value);
            if(cost[a][b]>value) cost[a][b]=cost[b][a]=value; //多条路的情况。
        }
        scanf("%d %d",&start,&endl);
        SPFA(start,endl);
        if(d[endl]!=INF) printf("%d
",d[endl]);
        else printf("-1
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yutingliuyl/p/7010885.html