HDU 1874 畅通工程续(Dijkstra)

题目地址:点击打开链接

思路:(1)起始点有可能相同(2)有可能起点到达不了终点(3)有重边的可能,要选出最短的边(4)城镇的编号是从0到n-1

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
const int maxn = 210;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int map1[maxn][maxn];
int lowdist[maxn],visit[maxn];
int n,m;
int qi,zhong;
void Dijkstra()
{
    memset(visit,0,sizeof(visit));
    for(int i=1; i<=n; i++)
        lowdist[i] = map1[qi][i];
    visit[qi] = 1;
    for(int i=1; i<n; i++)
    {
        int min1 = inf,k=-1;
        for(int j=1; j<=n; j++)
        {
            if(!visit[j] && lowdist[j] < min1)
            {
                min1 = lowdist[j];
                k = j;
            }
        }
        if(k == -1)
            break;
        visit[k] = 1;
        for(int j=1; j<=n; j++)
        {
            if(!visit[j] && map1[k][j] != inf)
            {
                if(lowdist[k] + map1[k][j] < lowdist[j])
                    lowdist[j] = lowdist[k] + map1[k][j];
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m) != EOF)
    {
        memset(map1,inf,sizeof(map1));
        for(int i=0; i<m; i++)
        {
            int st,ed,value;
            scanf("%d%d%d",&st,&ed,&value);
            st += 1;
            ed += 1;
            if(map1[st][ed] > value)
                map1[st][ed] = map1[ed][st] = value;
        }
        scanf("%d%d",&qi,&zhong);
        qi += 1;
        zhong += 1;
        if(qi == zhong)
        {
            printf("0\n");
            continue;
        }
        Dijkstra();
        if(lowdist[zhong] != inf)
            printf("%d\n",lowdist[zhong]);
        else
            printf("-1\n");
    }
    return 0;
}


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int a[210][210],dist[110],visit[110];
int n,m,start,end1;
const int maxn = 100000;

void dijkstra()
{
    int x,y,z;
    int i,j,min1,k;
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            a[i][j] = maxn;
        }
    }
    for(i=1; i<=m; i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        if(z < a[x][y])
        {
            a[x][y] = z;//防止重边
            a[y][x] = z;
        }
    }
    scanf("%d%d",&start,&end1);
    for(i=0; i<n; i++)
    {
        dist[i] = a[start][i];
        visit[i] = 0;
    }
    dist[start] = 0;
    visit[start] = 1;
    for(i=1; i<n; i++)//循环n-1次即可,一次求一个点,求n-1次
    {
        min1 = 1000000;
        for(j=0; j<n; j++)
        {
            if(visit[j] == 0 && dist[j] < min1)
            {
                k = j;
                min1 = dist[j];
            }
        }
        visit[k] = 1;
        if(k == end1)//求到退出来就行
            break;
        for(j=0; j<n; j++)
        {
            if(visit[j] == 0 && dist[k] + a[k][j] < dist[j])
                dist[j] = dist[k] + a[k][j];
        }
    }
}

int main()
{
    while(scanf("%d%d",&n,&m) != EOF)
    {
        dijkstra();
        if(dist[end1] == maxn)
            printf("-1\n");
        else
            printf("%d\n",dist[end1]);
    }
    return 0;
}


你可能感兴趣的:(HDU 1874 畅通工程续(Dijkstra))