2021-05-11正边权单源最短路+最小生成树

849. Dijkstra求最短路 I

1.题意:
2.题解:

稠密图
在这里插入图片描述

3.ac代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=510;
const int INF=0x3f3f3f3f;
int g[N][N],dist[N];
int st[N];
int n,m;
int dijistra(){
    dist[1]=0;
    
    for(int i=0;i<n;i++){
        int t=-1;
        for(int j=1;j<=n;j++){
            if(!st[j]&&(t==-1||dist[t]>dist[j])){
                t=j;
            }
        }
        st[t]=true;
        for(int j=1;j<=n;j++){
            if(dist[j]>dist[t]+g[t][j]){
                dist[j]=dist[t]+g[t][j];
            }
        }
    }
    if(dist[n]==INF) return -1;
    return dist[n];
    
}
int main(){
    cin>>n>>m;
    int x,y,z;
    memset(dist,INF,sizeof dist);
    memset(g,INF,sizeof g);
    for(int i=1;i<=m;i++){
        cin>>x>>y>>z;
        g[x][y]=min(g[x][y],z);
        
    }
    //dijistra();
    cout<<dijistra()<<endl;
    
}

850. Dijkstra求最短路 II

1.题意:
2.题解:

稀疏图,堆优化的邻接表,否则会t

3.ac代码:
#include<bits/stdc++.h>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N=2e5+10;
const int INF=0x3f3f3f3f;
int e[N],w[N],ne[N],h[N],idx;
int dist[N];

int n,m;
bool st[N];
void add(int a,int b,int c){
    e[idx]=b;w[idx]=c;ne[idx]=h[a];h[a]=idx++;
}
int dijkstra(){
    dist[1]=0;
    priority_queue<PII,vector<PII>,greater<PII>> heap;
    heap.push({0,1});
    while(heap.size()){
        PII tp=heap.top();
        heap.pop();
        int t=tp.second,d=tp.first;
        if(st[t]){
            continue;
        }
    
        st[t]=true;
        for(int j=h[t];j!=-1;j=ne[j]){
            int u=e[j];
            dist[u]=min(dist[u],dist[t]+w[j]);
            heap.push({dist[u],u});
        }
    }
    if(dist[n]==INF) return -1;
    return dist[n];
}
int main(){
    cin>>n>>m;
    memset(dist,INF,sizeof dist);
    memset(h,-1,sizeof h);
    memset(ne,-1,sizeof ne);
    int a,b,c;
    for(int i=1;i<=m;i++){
        cin>>a>>b>>c;
        add(a,b,c);
    }
    cout<<dijkstra()<<endl;
}

858. Prim算法求最小生成树

1.题意:
2.题解:
3.ac代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
int dist[N];
bool st[N];
int n,m;
int g[510][510];
int Prim(){
    int res=0;
    for(int i=0;i<n;i++){
        int t=-1;
        for(int j=1;j<=n;j++){
            if(!st[j]&&(t==-1||dist[t]>dist[j])){
                t=j;
            }
        }
        
        st[t]=true;
        if(i&&dist[t]==INF) return INF;//没有连通
        if(i)//只要不是第一个点
            res+=dist[t];
        for(int j=1;j<=n;j++){
            dist[j]=min(dist[j],g[t][j]);//dijkstra里面是dist[t]+g[t][j];
        }
    }
    return res;
    
}
int main(){
    cin>>n>>m;
    int u,v,w;
    memset(g,INF,sizeof g);
    memset(dist,INF,sizeof dist);
    for(int i=1;i<=m;i++){
        cin>>u>>v>>w;
        g[u][v]=g[v][u]=min(g[u][v],w);
    }
    int res=Prim();
    if(res==INF){
        cout<<"impossible"<<endl;
    }else{
        cout<<res<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值