HHU 3919 Travel spfa与dijkstra 解题报告

描述

There are N cities in this country, and some of them are connected by roads. Given two cities, can you find the shortest distance between them?

输入

The first line contains the number of test cases, then some test cases followed.

The first line of each test case contain two integers N and M (3 ≤ N ≤ 1000, M ≥ 0), indicating the number of cities and the number of roads. The next line contain two numbers S and T (S ≠ T), indicating the start point and the destination. Each line of the following M lines contain three numbers AiBi and Ci (1 ≤ Ai,Bi ≤ N, 1 ≤ Ci ≤ 1000), indicating that there is a road between city Ai and city Bi, whose length is Ci. Please note all the cities are numbered from 1 to N.

The roads are undirected, that is, if there is a road between A and B, you can travel from A to B and also from B to A.

输出

Output one line for each test case, indicating the shortest distance between S and T. If there is no path between them, output -1.

样例输入

2
3 3
1 3
1 2 10
2 3 20
1 3 50
3 1
1 3
1 2 10

样例输出

30
-1

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 1010

int map[MAXN][MAXN];
int dis[MAXN],vis[MAXN];
int n,m,a,b,c;

int spfa(int s)
{
    for(int i=1; i<=n; i++)
        dis[i]=INF;
    dis[s]=0;
    vis[s]=1;
    queue<int>Q;
    Q.push(s);
    while(!Q.empty())
    {
        int t=Q.front();
        Q.pop();
        vis[t]=0;
        for(int i=1; i<=n; i++)
        {
            if(dis[i]>dis[t]+map[t][i])
            {
                dis[i]=dis[t]+map[t][i];
                if(!vis[i])
                {
                    vis[i]=1;
                    Q.push(i);
                }
            }
        }
    }
    return 1;
}

int dijkstra(int s)
{
    for(int i=1;i<=n;i++)
        dis[i]=map[s][i];
    vis[s]=1;
    dis[s]=0;
    for(int i=2;i<=n;i++)
    {
        int minx=INF;
        int k=-1;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&(dis[j]<minx))
            {
                minx=dis[j];
                k=j;
            }
        }
        if(k==-1) return 0;
        vis[k]=1;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&(dis[j]>dis[k]+map[k][j]))
                dis[j]=dis[k]+map[k][j];
        }
    }
    return 1;
}


int main()
{
    int T,s,e;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        scanf("%d%d",&s,&e);
        memset(map,0x3f,sizeof(map));
        memset(vis,0,sizeof(vis));
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            map[a][b]=map[b][a]=c;
        }
        //spfa(s);
        dijkstra(s);
        printf("%d\n",dis[e]!=INF?dis[e]:-1);
    }

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值