图论算法------Dijkstra

1.Dijkstra(邻接矩阵)

2.Dijkstra(邻接表)

3.Dijkstra(邻接表的堆优化)

4.快速幂

1.

#include<iostream>
#include<cstring>

using namespace std;
const int N = 505;
int graph[N][N], st[N], dist[N];
int m, n;

void dikstra()
{
    memset(dist, 0x3f, sizeof dist);//初始化为正无穷
    dist[1] = 0;//从一到一距离为0
    // 
    for(int j = 0; j < n; j++)
    {
        int t = -1;
        for(int i = 1; i <= n; i++)
        {
            if(!st[i] && (t == -1 || dist[t] > dist[i]))
            {
                t = i;//找到离顶点最近的点
            }
        }
        if(t == -1)
        {
            return ;
        }
        st[t] = 1;//标记为已访问
        for(int i = 1; i <= n; i++)
        {
            if(dist[t] + graph[t][i] < dist[i]) // dist[t] 表示  1 -> t 的最短距离  1 -> t +  t - > i    1 -> i
            {
                dist[i] =  dist[t] + graph[t][i];//更新点的距离
            }
        }
    }

}

int main()
{
    cin>>n>>m;
    memset(graph, 0x3f, sizeof graph);
    for(int i = 0; i < m; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        graph[a][b] = min(graph[a][b], c); //避免重边
    }
    dikstra();
    if(dist[n] != 0x3f3f3f3f) cout<<dist[n];//判断是否能到达
    else cout<<-1;
}

2.

#include<bits/stdc++.h>
using namespace std;

const int N=505; 
typedef pair<int,int> PII;//起名字
vector<PII> q[N];//二维vector套pair
int st[N],dist[N];
int t;
int n,m;

void add(int a,int b,int c){
    q[a].push_back({c,b});//表示从a到b长度为c
}

void DJ(){
    memset(dist,0x3f,sizeof(dist));
    dist[1]=0;
    for(int j=0;j<n;j++){
        t=-1;
        for(int i=1;i<=n;i++){
            if(!st[i] && (t==-1 || dist[t]>dist[i])){
                t=i;
            }
        }
        if(t==-1){
            return ;
        }
        st[t]=1;
        for(int i=0;i<q[t].size();i++){
            int j = q[t][i].second;
            if(dist[t]+q[t][i].first<dist[j]){
                dist[j]=dist[t]+q[t][i].first;//一样
            }
        }
    }
    


int main(){
    cin>>n>>m;
    
    for(int i=0;i<m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        add(a,b,c);
    }
    DJ();
    //for(int i = 1; i <= n; i++) cout<<dist[i]<<" "; 
    if(dist[n] != 0x3f3f3f3f){
        cout<<dist[n];
        return 0;
    }
    cout<<-1;
    
    return 0;
}

3.

#include<bits/stdc++.h>
using namespace std;

const int N=505; 
typedef pair<int,int> PII;//起名字 
priority_queue<PII, vector<PII>, greater<PII> > q;//小顶堆 会自动按第一关键字排序
vector<PII> graph[N];
int st[N],dist[N];
int t;
int n,m;
int s;
 
void add(int a,int b,int c){
    graph[a].push_back({c,b});
}

void DJ(int s){
    memset(dist,0x3f,sizeof(dist));
    dist[s]=0;//起始点到自己是0
    q.push({0, s});
    while(!q.empty())
    {
        PII f = q.top();
        q.pop();
        int t = f.second;
        if(st[t]) continue;    //若访问过,则跳过
        st[t]=1;//若还没访问则标记为已访问
        for(int i=0;i<graph[t].size();i++){
            int j = graph[t][i].second;
            if(dist[t]+graph[t][i].first<dist[j]){
                dist[j]=dist[t]+graph[t][i].first;
                q.push({dist[j],j});
            }
        }
    }    


int main(){
    cin>>n>>m;
    
    for(int i=0;i<m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        add(a,b,c);
    }
    DJ(1);

    if(dist[n] != 0x3f3f3f3f){
        cout<<dist[n];
        return 0;
    }
    cout<<-1;
    
    return 0;
}
 

4.

#include<bits/stdc++.h>
using namespace std;


int pow3(int x, int n){
    if(n == 0) return 1;
    int t = 1;
    while(n != 0){
      if(n & 1) t *= x;
      n >>= 1;
      x *= x;
    }
    return t;
}

int main(){
    int n,m;
    cin>>n>>m;
    cout<<pow3(n,m);
    return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值