dijstra邻接矩阵和邻接表

问题描述
所以草儿决定在要在最短的时间去一个自己想去的地方!
因为草儿的家在一个小镇上,没有火车经过,所以她只能去邻近的城市坐火车。

输入描述
输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个,草儿想去的地方有D个;
接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time小时;(1=<(a,b)<=1000;a,b 之间可能有多条路)
接着的第T+1行有S个数,表示和草儿家相连的城市;
接着的第T+2行有D个数,表示草儿想去地方。

输出描述
输出草儿能去某个喜欢的城市的最短时间。

输入样例
6 2 3
1 3 5
1 4 7
2 8 12
3 8 4
4 9 12
9 10 2
1 2
8 9 10

输出样例
9

震惊这道题其实一次Dijkstra就可以了,我们将草儿的家看做0,从草儿家到相邻镇的花费看做0,那么我们就只需要求草儿家到各个目的地的最短路即可,一次Dijkstra便可解决
设置一个点 使其到起点的距离为0,再逐步更新其他的点?

dijstra邻接矩阵

很显然邻接矩阵简单一些 但是数据大的话 就十分的不管用了。

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int Ni = 1005;
int graph[1005][1005];
int dis[Ni], n, Max = 0;
bool visited[1005];

struct node{
    int a, w;
    node(){}
    node(int m, int n){a=m; w=n;}
    bool operator < (const node & x) const
    {
        if(w==x.w) return a<x.a; //返回另一个顶点小的那个 
        else return w > x.w; //返回权值小的那个 
    }
};

void Dijkstra(int s) //源点 
{
    for(int i = 0; i <= Max; i++)
    	visited[i] = false;
    priority_queue<node> q;
    dis[s] = 0;
    q.push(node(s, dis[s]));  
    while(!q.empty())
    {
        node x = q.top();
        q.pop();
        if(visited[x.a])
            continue;
        visited[x.a] = true;
        for(int v = 1; v <= Max; v++) //找所有与他相邻的顶点,进行松弛操作,更新估算距离,压入队列。
        {
            if(v != x.a && !visited[v] && dis[v] > dis[x.a]+graph[x.a][v])
            {
                dis[v] = dis[x.a]+graph[x.a][v];
                q.push(node(v, dis[v]));
            }
        }
    }
}
int main(){
    int T, S, D, a, b, w;
    while(scanf("%d%d%d",&T, &S, &D) != EOF)
    {
        memset(dis, INF, sizeof(dis));
        memset(graph, INF, sizeof(graph));		
        while(T--)//T条边 
        {
            scanf("%d%d%d",&a,&b,&w);
            graph[a][b] = min(graph[a][b], w);
            graph[b][a] = min(graph[b][a], w);
            Max = max(Max, max(a, b));//没给最大的点 不然就不用求了 
        }
        for(int i = 0; i < S; i++)
        {
            scanf("%d", &n);
            Dijkstra(n);
//          for(int i = 1; i <= 10; i++)
//                printf("%d ", dis[i]);
//          printf("\n");
        }
        int min = INF;
        for(int i = 0; i < D; i++)
        {
            scanf("%d", &n);
            if(min > dis[n])
                min = dis[n];
        } 
        printf("%d\n", min); 
    }
    return 0;
}

dijstra邻接表

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#define INF  0x3f3f3f3f
using namespace std;
const int Ni = 10000;

struct node{
    int v,w;
    node(){}
    node(int a,int b){v=a; w=b;}
    bool operator < (const node & a) const
    {
        if(w==a.w) return v<a.v; //返回另一个顶点小的那个 
        else return w > a.w; //返回权值小的那个 
    }
};

vector<node> eg[Ni]; //每个点都有一个向量存其临边 
int dis[Ni],n;
void Dijkstra(int s)
{
    int i;
    for(i=0;i<=n;i++) dis[i]=INF;
    dis[s]=0;
    //用优先队列优化
    priority_queue<node> q;
    q.push(node(s,dis[s]));  
    while(!q.empty())
    {
        node x=q.top();q.pop();
        for(i=0;i<eg[x.v].size();i++)
        {
            node y=eg[x.v][i];
            if(dis[y.v]>x.w+y.w)
            {
                dis[y.v]=x.w+y.w;
                q.push(node(y.v,dis[y.v]));
            }
        }
    }
}
int main()
{
    int a,b,d,m;
    while(scanf("%d%d",&n,&m),n+m)
    {
        for(int i=0;i<=n;i++) eg[i].clear();
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&d);
            eg[a].push_back(node(b,d));
            eg[b].push_back(node(a,d));
            //题目上需要更新 node的距离 有点麻烦了 要搜索所有的临边 所以不在这题采用邻接表了 
        }
        Dijkstra(1);
        printf("%d\n",dis[n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37430374/article/details/88769714