HDU_1874-畅通工程续 Dijkstra模板(链式向前星 + pair堆优化) + Spfa模板

本文探讨了在给定多个城镇和道路的情况下,如何利用最短路径算法找到从一个城镇到另一个城镇的最短距离。通过具体示例,介绍了Dijkstra算法和SPFA算法的实现过程。

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

题目链接
畅通工程续
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 72085 Accepted Submission(s): 27916

Problem Description
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。

Input
本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。

Output
对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.

Sample Input

3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2

Sample Output

2
-1

Author
linle

Source
2008浙大研究生复试热身赛(2)——全真模拟

Recommend
lcy | We have carefully selected several similar problems for you: 2544 2066 2112 1217 1875

/*
* @Author: Achan
* @Date:   2018-10-26 17:58:34
* @Last Modified by:   Achan
* @Last Modified time: 2018-10-26 20:30:03
*/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<vector> 
#include<queue>
#include<cmath> 
using namespace std;

#define X             first
#define Y             second
#define eps          1e-2
#define gcd         __gcd
#define pb             push_back
#define PI             acos(-1.0)
#define lowbit(x)     (x)&(-x)
#define fin         freopen("in.txt","r",stdin);
#define fout        freopen("out.txt","w",stdout);
#define bug         printf("!!!!!\n");
#define mem(x,y)    memset(x,y,sizeof(x))
#define rep(i,j,k)  for(int i=j;i<(int)k;i++)
#define per(i,j,k)  for(int i=j;i<=(int)k;i++)
#define io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);

typedef long long ll;
typedef long double LD; 
typedef pair<int,int> pii;
typedef unsigned long long ull; 

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const int inf  = 1<<30;
const ll  INF  = 1e18 ;
const int mod  = 1e9+7;
const int maxn = 2e2+2;
int head[maxn], dis[maxn];
bool vis[maxn];
struct node 
{
    int to;
    int nex;
    int w; 
}edge[maxn*maxn];
int top;
int n;
void init() {memset(head,-1,sizeof(head)); top = 0;}

inline void add_edge(int u, int v, int w) //链式向前星图
{
    edge[top].to = v;
    edge[top].w  = w;
    edge[top].nex = head[u];
    head[u] = top++;
   
    
    edge[top].to = u;
    edge[top].w  = w;
    edge[top].nex = head[v];
    head[v] = top++;
   
}
void Dijkstra(int s)
{
    memset(vis,0,sizeof(vis));
    fill(dis,dis+maxn,inf);
    priority_queue <pair<ll, int> > q;
    q.push(make_pair(0,s)); 
    dis[s] = 0;
    while(!q.empty())
    {
        int cur  = q.top().second; 
        q.pop();
        if(vis[cur]) continue;
        vis[cur] = 1;
        for(int i = head[cur]; ~i; i=edge[i].nex)
        {
            int w = edge[i].w; 
            int to = edge[i].to; 
            if(!vis[to] && dis[to] > dis[cur] + w)
            {
                dis[to] = dis[cur] + w; 
                // cout<<"from : "<<cur<<" to :"<<to<<" val :"<<dis[to]<<endl;
                q.push(make_pair(-dis[to], to)); 
            }
        }
    }
}
int main(void)
{
    int m;
    while(cin>>n>>m)
    {
        init();
        int u,v,w,s,t; 
        while(m--)
        {
            // cin>>u>>v>>w;
            u = read() , v = read(), w = read();
            add_edge(u,v,w);
        }
        // for(int i = 0 ;i<top; i++)
        //     cout<<edge[i].nex<<" "<<edge[i].to<<" "<<edge[i].w<<endl;    
        cin>>s>>t;
        Dijkstra(s);
        cout<<( dis[t] == inf? -1:dis[t])<<endl;
    }
}

Spfa

/*
* @Author: Achan
* @Date:   2018-10-26 17:58:34
* @Last Modified by:   Achan
* @Last Modified time: 2018-10-29 17:38:41
*/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<vector> 
#include<queue>
#include<cmath> 
using namespace std;

#define X             first
#define Y             second
#define eps          1e-2
#define gcd         __gcd
#define pb             push_back
#define PI             acos(-1.0)
#define lowbit(x)     (x)&(-x)
#define fin         freopen("in.txt","r",stdin);
#define fout        freopen("out.txt","w",stdout);
#define bug         printf("!!!!!\n");
#define mem(x,y)    memset(x,y,sizeof(x))
#define rep(i,j,k)  for(int i=j;i<(int)k;i++)
#define per(i,j,k)  for(int i=j;i<=(int)k;i++)
#define io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);

typedef long long ll;
typedef long double LD; 
typedef pair<int,int> pii;
typedef unsigned long long ull; 

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const int inf  = 1<<30;
const ll  INF  = 1e18 ;
const int mod  = 1e9+7;
const int maxn = 2e2+2;
int head[maxn], dis[maxn];
bool vis[maxn];
struct node 
{
    int to;
    int nex;
    int w; 
}edge[maxn*maxn];
int top;
int n;
void init() {memset(head,-1,sizeof(head)); top = 0;}

inline void add_edge(int u, int v, int w) //链式向前星图
{
    edge[top].to = v;
    edge[top].w  = w;
    edge[top].nex = head[u];
    head[u] = top++;
    
    edge[top].to = u;
    edge[top].w  = w;
    edge[top].nex = head[v];
    head[v] = top++;
}
void spfa(int s)
{
    memset(vis,0,sizeof(vis));
    fill(dis,dis+maxn,inf);
    dis[s] = 0;
    vis[s] = 1;
    queue <int> q;  
    q.push(s);
    while(!q.empty())
    {
        int cur = q.front();
        q.pop();
        vis[cur] = 0;  //从队列中pop出去的 设置可访问态,可以再次用于更新别的最短路!!!
        for(int i=head[cur]; ~i; i=edge[i].nex)
        {
            int w = edge[i].w;
            int to = edge[i].to;
            if(dis[to] > dis[cur] + w)
            {
                dis[to] = dis[cur] + w;
                if(!vis[to]) 
                {
                    vis[to] = 1;
                    q.push(to); 
                }
            }
        }

    }
}
int main(void)
{

    // fout
    io
    int m;
    while(cin>>n>>m)
    {
        init();
        int u,v,w,s,t; 
        while(m--)
        {
            // cin>>u>>v>>w;
            u = read() , v = read(), w = read();
            add_edge(u,v,w);
        }
        // for(int i = 0 ;i<top; i++)
        //     cout<<edge[i].nex<<" "<<edge[i].to<<" "<<edge[i].w<<endl;    
        cin>>s>>t;
        spfa(s); 
        cout<<( dis[t] == inf? -1:dis[t])<<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值