HUD 1874 畅通工程续(Dijkstra/spfa/floyd)

Problem Description
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

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

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
 
版本一:Floyd(时间复杂度O(n^3))
<span style="font-size:10px;">//Must so
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<ctype.h>
#include<queue>
#include<vector>
#include<set>
#include<cstdio>
#include<cmath>
#define mem(a,x) memset(a,x,sizeof(a))
#define inf 1<<25
#define NN 1000006
using namespace std;
const double PI = acos(-1.0);
typedef long long LL;

/**********************************************************************
练手很好,可以尝试 Dijkstra ,spfa,Floyd
尝试1:Floyd
**********************************************************************/
int s,t;//起点和终点
int n,m;//n个点,m条边
int d[202][202];
void init()
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            d[i][j] = inf;
        }
        d[i][i] = 0;
    }
}
void floyd()
{
    for (int k = 0; k < n; k++)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
            }
        }
    }
}
int main()
{
    while (cin>>n>>m)
    {
        init();
        for (int i = 0,u,v,w; i < m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            if (w < d[u][v])
            {
                d[u][v] = w;
                d[v][u] = w;//双向
            }
        }
        cin>>s>>t;
        floyd();
        if (d[s][t] == inf)
        {
            puts("-1");
        }
        else
        {
            printf("%d\n",d[s][t]);
        }
    }
    return 0;
}</span><span style="font-size:18px;">
</span>
版本二:SPFA(不定长数组的邻接矩阵实现)
<span style="font-size:10px;">//Must so
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<ctype.h>
#include<queue>
#include<vector>
#include<set>
#include<cstdio>
#include<cmath>
#define mem(a,x) memset(a,x,sizeof(a))
#define inf 1<<25
#define NN 1000006
using namespace std;
const double PI = acos(-1.0);
typedef long long LL;

/**********************************************************************
练手很好,可以尝试 Dijkstra ,spfa,Floyd
尝试2:SPFA(不定长数组实现邻接矩阵)
**********************************************************************/
int s,t;//起点和终点
int n,m;//n个点,m条边
struct Node
{
    int v,w;
}a;
bool vis[202];
vector<Node>u[202];
queue<int>q;
int d[202];
void spfa()
{
    for (int i = 0;i < n;i++)
        d[i] = inf;
    mem(vis,0);
    q.push(s);
    vis[s] = 1;
    d[s] = 0;//源点最先入队并标记
    while (!q.empty())
    {
        int head = q.front();//取队首元素
        q.pop();
        vis[head] = 0;
        for (int i = 0;i < u[head].size();i++)
        {
            if (d[ u[head][i].v ] > d[head] + u[head][i].w)//松弛成功
            {
                d[ u[head][i].v ] = d[head] + u[head][i].w;//更新值
                if (vis[u[head][i].v] == 0)
                {
                    q.push(u[head][i].v);//入队
                    vis[u[head][i].v] = 1;
                }
            }
        }
    }
}
int main()
{
    while (cin>>n>>m)
    {
        for (int i = 0;i < n;i++)//不定长数组记得清空
            u[i].clear();
        for (int i = 0,uu; i < m; i++)
        {
            scanf("%d%d%d",&uu,&a.v,&a.w);
            u[uu].push_back(a);
            Node b;
            b.v = uu,b.w = a.w;
            u[a.v].push_back(b);
        }
        cin>>s>>t;
        spfa();
        if (d[t] == inf) puts("-1");
        else printf("%d\n",d[t]);
    }
    return 0;
}
</span>


版本三:SPFA(二维数组实现邻接矩阵,这题点少,用二维数组时间反而比不定长数组少)
<span style="font-size:10px;">//Must so
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<ctype.h>
#include<queue>
#include<vector>
#include<set>
#include<cstdio>
#include<cmath>
#define mem(a,x) memset(a,x,sizeof(a))
#define inf 1<<25
#define NN 1000006
using namespace std;
const double PI = acos(-1.0);
typedef long long LL;

/**********************************************************************
练手很好,可以尝试 Dijkstra ,spfa,Floyd
尝试2:SPFA(二维数组实现邻接矩阵)
**********************************************************************/
int s,t;//起点和终点
int n,m;//n个点,m条边
bool vis[202];
int mp[202][202];
queue<int>q;
int d[202];
void spfa()
{
    for (int i = 0; i < n; i++)
        d[i] = inf;
    mem(vis,0);
    q.push(s);
    vis[s] = 1;
    d[s] = 0;//源点最先入队并标记
    while (!q.empty())
    {
        int head = q.front();//取队首元素
        q.pop();
        vis[head] = 0;
        for (int i = 0; i < n; i++)
        {
            if (d[i] > d[head] + mp[head][i])//松弛成功
            {
                d[i] = d[head] + mp[head][i];//更新值
                if (vis[i] == 0)
                {
                    q.push(i);//入队
                    vis[i] = 1;
                }
            }
        }
    }
}
int main()
{
    while (cin>>n>>m)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                mp[i][j] = inf;
            }
            mp[i][i] = 0;
        }
        for (int i = 0,uu,vv,ww; i < m; i++)
        {
            scanf("%d%d%d",&uu,&vv,&ww);
            if (ww < mp[uu][vv])
            {
                mp[uu][vv] = ww,mp[vv][uu] = ww;
            }
        }
        cin>>s>>t;
        spfa();
        if (d[t] == inf) puts("-1");
        else printf("%d\n",d[t]);
    }
    return 0;
}
</span>

版本四:SPFA(邻接表实现)

//Must so
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<ctype.h>
#include<queue>
#include<vector>
#include<set>
#include<cstdio>
#include<cmath>
#define mem(a,x) memset(a,x,sizeof(a))
#define inf 1<<25
#define NN 1000006
using namespace std;
const double PI = acos(-1.0);
typedef long long LL;

/**********************************************************************
练手很好,可以尝试 Dijkstra ,spfa,Floyd
尝试2:SPFA(邻接表实现)
**********************************************************************/
int s,t;//起点和终点
int n,m;//n个点,m条边
struct Node
{
    int v,w,next;
}e[2002];//因为双向的原因边最多有2000条
int h[102];
bool vis[202];
queue<int>q;
int d[202];
void spfa()
{
    for (int i = 0; i < n; i++)
        d[i] = inf;
    mem(vis,0);
    q.push(s);
    vis[s] = 1;
    d[s] = 0;//源点最先入队并标记
    while (!q.empty())
    {
        int head = q.front();//取队首元素
        q.pop();
        vis[head] = 0;
        for (int i = h[head]; i != -1; i = e[i].next)
        {
            if (d[e[i].v ] > d[head] +e[i].w)//松弛成功
            {
                d[e[i].v ] = d[head] + e[i].w;//更新值
                if (vis[e[i].v] == 0)
                {
                    q.push(e[i].v);//入队
                    vis[e[i].v] = 1;
                }
            }
        }
    }
}
int main()
{
    while (cin>>n>>m)
    {
        mem(h,-1);
        int o = 0;
        for (int i = 0,uu,vv,ww; i < m; i++)
        {
            scanf("%d%d%d",&uu,&vv,&ww);
            e[o].v = vv;
            e[o].w = ww;
            e[o].next = h[uu];
            h[uu] = o++;
            e[o].v = uu;
            e[o].w = ww;
            e[o].next = h[vv];
            h[vv] = o++;
        }
        cin>>s>>t;
        spfa();
        if (d[t] == inf) puts("-1");
        else printf("%d\n",d[t]);
    }
    return 0;
}
版本五:Dijkstra
//Must so
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<ctype.h>
#include<queue>
#include<vector>
#include<set>
#include<cstdio>
#include<cmath>
#define mem(a,x) memset(a,x,sizeof(a))
#define inf 1<<25
#define NN 1000006
using namespace std;
const double PI = acos(-1.0);
typedef long long LL;

/**********************************************************************
练手很好,可以尝试 Dijkstra ,spfa,Floyd
尝试3:Dijkstra
**********************************************************************/
int s,t;//起点和终点
int n,m;//n个点,m条边
bool vis[202];
int d[202];
int mp[202][202];
void Dijk()
{
    mem(vis,0);
    for (int i = 0; i < n; i++)
    {
        int mn = inf,pos;
        for (int j = 0; j < n; j++)
        {
            if (d[j] < mn&&vis[j] == 0)
            {
                mn = d[j];
                pos = j;
            }
        }
        vis[pos] = 1;
        for (int j = 0; j < n; j++)
        {
            d[j] = min(d[j],d[pos]+mp[pos][j]);
        }
    }
}
int main()
{
    while (cin>>n>>m)
    {
        for (int i = 0;i < n;i++)
        {
            for (int j = 0;j < n;j++)
            {
                mp[i][j] = inf;
            }
            mp[i][i] = 0;
            d[i] = inf;
        }
        for (int i = 0,uu,vv,ww; i < m; i++)
        {
            scanf("%d%d%d",&uu,&vv,&ww);
            if (ww < mp[uu][vv])
            {
                mp[uu][vv] = ww,mp[vv][uu] = ww;
            }
        }
        cin>>s>>t;
        d[s] = 0;
        Dijk();
        if (d[t] == inf) puts("-1");
        else printf("%d\n",d[t]);
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值