(step6.2.4)hdu 2680(Choose the best route——最短路径)

本文介绍了一种解决单源最短路径问题的方法,通过添加一个超级源点将多个起点的问题转换为标准的单源最短路径问题。适用于有向图,并提供了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:输入3个整数n,m,end。分别表示村庄数、道路数以及终点。在接下来的m行中

,每一行输入3个整数a,b,c。分别表示每条道路的起点、终点,以及所要花费的时间。

在接下来输入一个数字count,表示可以选择的起点的个数。在接下来的一行中有count个数字

,每个数字表示可以选择的起点的标号。


解题思路:最短路径

1)本题是单向的。不要写成双向的。很可能会WA。

2)设置一个超级源点来连接多个起点。将本题转化成基本题来做即可。如果是采用循环多个起点的情况,则很可能会TLE。


以下是其他大牛写的题目大意与解题思路:

题目大意:

给你一个有向图,一个起点集合,一个终点,求最短路。。。。

解题思路:

1.自己多加一个超级源点,把起点集合连接到超级源点上,然后将起点与超级源点的集合的路径长度设为0,这样就称为一个n+1个点的单源最短路算法。。。。。




代码如下:

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>

using namespace std;
#define inf 10000000000000
__int64 map[1010][1010];
__int64 d[1010];
bool hash[1010];
__int64 a[1010];
__int64 l1,l2,l3,l4,c1,c2,c3,c4;
__int64 n,m;
__int64 start,end;

typedef struct node{
   __int64 adj;
   __int64 weight;
   node* next;
}node,*pnode;

typedef struct Heap{
    bool operator<(Heap T)const{
       return T.dis < dis;
    }
   __int64 x;
   __int64 dis;
}Heap;

priority_queue<Heap> Q;

__int64 bfs(){
   __int64 i;
   for(i = 0 ; i <= n ; ++i){
       hash[i] = 0;
       d[i] = inf;
   }

   Heap min,in;
   while(!Q.empty()){
    Q.pop();
   }

   in.x = start;
   in.dis = 0;
   d[start] = 0;
   Q.push(in);
   while(!Q.empty()){
     min = Q.top();
     Q.pop();

     if(min.x == end){
        return min.dis;
     }

     if(hash[min.x]){
        continue;
     }

     hash[min.x] = true;


     for(i = 0 ; i <= n ; ++i){
        if(d[i] > d[min.x] + map[min.x][i] && !hash[i]){
            in.x = i;
            in.dis = map[min.x][i] + d[min.x];
            d[i] = in.dis;
            Q.push(in);
        }
     }
   }
   return -1;
}

int judge(int dis){
   if(dis > 0 && dis <= l1){
      return c1;
   }else{
      if(dis > l1 && dis <= l2){
        return c2;
      }else{
        if(dis > l2 && dis <= l3){
            return c3;
        }else if(dis > l3 && dis <= l4){
            return c4;
        }else{
            return -1;
        }
      }
   }
}

int main(){
    while(scanf("%I64d%I64d%I64d",&n,&m,&end)!=EOF){
        __int64 i,j;
        for(i= 0 ; i <= n ; ++i){
            for(j = 0 ; j <= n ; ++j){
                map[i][j] = inf;
            }
        }

        __int64 a,b,c;
        for(i = 1 ; i <= m ; ++i){
            scanf("%I64d%I64d%I64d",&a,&b,&c);
            if(map[a][b] > c){

            map[a][b]  = c;//这里是单向的,别写成双向的.否则会WA。。。
            }
        }

        int count;
        scanf("%d",&count);
        int a1;
        /**
         * 以下是本体的巧妙之处:设置一个超级源点,链接多个起点
         */
        for(i = 1 ; i <= count ; ++i){
            scanf("%d",&a1);
            map[0][a1] = 0;
        }

        start = 0;
        bfs();

        if(d[end] == inf){
            printf("-1\n");
        }else{
            printf("%I64d\n",d[end]);
        }
    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值