Dijkstra迪杰斯特拉算法+贪心

最短路径问题。我们通过一道ccf真题来讲解。
问题描述
  G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家也建设一个高速铁路系统。
  建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改造成高速铁路。现在,请你为G国国王提供一个方案,将现有的一部分铁路改造成高速铁路,使得任何两个城市间都可以通过高速铁路到达,而且从所有城市乘坐高速铁路到首都的最短路程和原来一样长。请你告诉G国国王在这些条件下最少要改造多长的铁路。
输入格式
  输入的第一行包含两个整数n, m,分别表示G国城市的数量和城市间铁路的数量。所有的城市由1到n编号,首都为1号。
  接下来m行,每行三个整数a, b, c,表示城市a和城市b之间有一条长度为c的双向铁路。这条铁路不会经过a和b以外的城市。
输出格式
  输出一行,表示在满足条件的情况下最少要改造的铁路长度。
样例输入
4 5
1 2 4
1 3 5
2 3 2
2 4 3
3 4 2
样例输出
11
两种解法:
第一种定义一个数据结构:

 #include <iostream>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<stdio.h>
using namespace std;

 // v表示节点,cost表示出发点到v点的距离
typedef struct node{
    int v;
    int cost;
    node(int vv,int c){
        v =vv;cost = c;
    }

    // 优先队列将按距离从小到大排列
    friend bool operator<(node n1,node n2){
        return n1.cost>n2.cost;
    }
}node;

// v表示边的另一端节点,cost表示该边的权重
typedef struct edge{

    int v;
    int cost;
    edge(int vv,int c){
        v = vv;cost = c;
    }
}edge;
vector<edge>G[10005];//无向图
bool marked[10005];//标志数组,false表示该点没有被访问,true表示已经被访问

 接通该点需要增加的边的权重.即表示相邻点到该点需要花费的路径长度(此数组可以去掉重复边)
int costo[10005];
int disto[10005];//表示起点到该点的总距离(用于在计算的过程中取最小)


//用以记录到每一个点的最短距离所经过的点
/*
 *P[v][w] = 1表示起点到v点的途中经过w点
 *P[v][w] = 0表示起点到v点的途不中经过w点
 */
int P[10005][10005];
int n,m;
void dijstra(int sta){
    memset(P,0,sizeof(P));

    //初始化
    for(int i=1;i<=n;i++){
        costo[i] = disto[i] = INFINITY;
        marked[i] = false;
    }

    costo[sta] = disto[sta] = 0;
    priority_queue<node>q;
    q.push(node(sta,0));
    while(!q.empty()){
        node temp = q.top();//取最小值
        q.pop();
        int v = temp.v;
        P[v][v] = 1;

        //判断改点是否被访问
        if(!marked[v]){
            marked[v] = true;
            int len = G[v].size();
            for(int i=0;i<len;i++){
                int vv = G[v][i].v;
                if(marked[vv])continue;
                int cost = G[v][i].cost;
                int newdis = disto[v]+cost;
                if(disto[vv]>newdis){
                    disto[vv] = newdis;
                    costo[vv] = cost;
                    q.push(node(vv,newdis));
                    for(int f=1;f<=n;f++){
                        P[vv][f] = P[v][f];
                    }
                    P[vv][vv] = 1;
                }else if(disto[vv]==newdis){
                    if(costo[vv]>cost){
                        for(int f=1;f<=n;f++){
                            P[vv][f] = P[v][f];
                        }
                        P[vv][vv] = 1;

                        costo[vv] = cost;
                    }
                    //costo[vv] = min(costo[vv],cost);
                }
            }
        }
    }
}
int main()
{
    cin>>n>>m;
    for(int i=0;i<m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        G[a].push_back(edge(b,c));
        G[b].push_back(edge(a,c));
    }
    dijstra(1);
    int sum = 0;
    for(int i=1;i<=n;i++){
        //cout<<costo[i]<<endl;
        sum+=costo[i];
    }
    cout<<sum<<endl;
    for(int i=2;i<=n;i++){
        cout<<i<<":";
        for(int j=1;j<=n;j++){
            if(P[i][j]==1){
                cout<<j<<" ";
            }
        }
        cout<<endl;
    }
    return 0;
}

方法二:直接利用数组不用结构体

#include <iostream>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<stdio.h>

using namespace std;

//获取D数组中的最小值
int getMin(int d[],int finals[],int n){
    int flag = 0;
    int mins = INFINITY;
    for(int i=1;i<=n;i++){
        if(finals[i]==0&&mins>d[i]){
            //cout<<"mins"<<endl;
            mins = d[i];
            flag = i;
        }
    }
    return flag;
}
int main()
{
    int n,m;
    cin>>n>>m;
    int road[n+1][n+1];//表示边,值表示边的权重
    int D[n+1];//出发点到某点的距离
    int P[n+1][n+1];//路径数组
    int finals[n+1];//1表示归为s集合,0表示没有归为s集合
    int costo[n+1];//接通该点需要增加的边的权重
    for(int i=1;i<=n;i++){
            costo[i] = INFINITY;
        for(int j=1;j<=n;j++){
            road[i][j] = INFINITY;

        }
    }
    costo[1] = 0;
    for(int i=0;i<m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        road[a][b] = c;

    }

    for(int i=1;i<=n;i++){
        finals[i] = 0;//是否被收入s
        D[i] = road[1][i];//初始化距离和

    }
    //初始化
    D[1] = 0;road[1][1] = 0;

    //制空路径,初始化P数组
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            P[i][j] = 0;
        }
        if(D[i]<INFINITY){P[i][1] = 1;P[i][i] = 1;}
    }

    //开始Dijkstra算法
    for(int i=2;i<=n;i++){
        int k = getMin(D,finals,n);//获取路径最小值的索引
        int mins = D[k];
        //cout<<mins<<endl;
        finals[k] = 1;
        P[k][k] = 1;//计入路径组
        //cout<<n;


        //更新D数组
        for(int j=1;j<=n;j++){
            if(finals[j]==0&&(mins+road[k][j])<D[j]){
                costo[j] = road[k][j];
                D[j] = mins+road[k][j];
                for(int e=1;e<=n;e++){
                    P[j][e] = P[k][e];
                }
                P[j][j] = 1;

            }else if(finals[j]==0&&(mins+road[k][j])==D[j]){
                costo[j] = min(costo[j],road[k][j]);

            }

        }

    }

    int sum = 0;
    for(int i=1;i<=n;i++){
        //cout<< costo[i]<<","<<endl;
        sum+=costo[i];//去掉重复边的长度
    }
    cout<<sum<<endl;

    //也可以输出路径。P数组

    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值