跳到正文

题解

P17184 题解

Tommyy约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年8月3日
收录
2026年8月4日
更新
2026年10月31日
篇幅
约 9 分钟
获赞
1
题目
P17184

版本

  1. v1当前

P17184 题解

本题考查最短路算法,是一道无向图最短路问题应为数据小, 可以用Floyd 算法过,没有负权所以也可以用dijkstar算法过;

Floyd 版本
C++
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 55;
int dis[N][N];

int main()
{
    int m;
    while (cin >> m)
    {
        int s,t;
        cin>>s>>t;
        for (int i=1;i<=m;i++)
        {
        	for (int j=1;j<=m;j++)
        	{
        		dis[i][j]=(i==j)?0:INF;
			}
                
		}
            
        
        int a,b,c;
        while (cin>>a)
        {
            if (a == 0) 
			{
				break;
			}
            cin>>b>>c;
            dis[a][b]=min(dis[a][b],c);
            dis[b][a]=min(dis[b][a],c);
        }

        for (int k=1;k<=m;k++)
        {
        	for (int i = 1; i <= m;i++)
            {
            	for (int j = 1; j <= m;j++)
                {
                	dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]);
				}
			}
		}
            
                
                    
        
        cout<<dis[s][t]<<'\n';
    }
    return 0;
}

AC记录

Dijkstra 版本(单源最短路)
C++
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int N = 55;
vector<PII> edge[N];
int dis[N];
bool vis[N];

int main()
{
    int m;
    while (cin >> m)
    {
        for (int i = 1; i <= m;i++) 
		{
			edge[i].clear();
		}
        int s, t;
        cin>>s>>t;
        int a,b,c;
        while (cin >> a)
        {
            if (a==0) 
			{
				break;
			}
            cin>>b>>c;
            edge[a].emplace_back(b, c);
            edge[b].emplace_back(a, c);
        }

        memset(dis,0x3f,sizeof dis);
        memset(vis,0,sizeof vis);
        priority_queue<PII,vector<PII>,greater<PII>> q;
        dis[s] = 0;
        q.emplace(0, s);
        while (!q.empty())
        {
            auto [d,u]=q.top();
			q.pop();
            if (vis[u]) 
			{
				continue;
			}
            vis[u]=true;
            for (auto [v,w]:edge[u])
            {
                if (dis[v]>d+w)
                {
                    dis[v]=d+w;
                    q.emplace(dis[v], v);
                }
            }
        }
        cout<<dis[t]<<'\n';
    }
    return 0;
}

AC记录

Bellman-Ford 算法
C++
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
struct Node{
    int u,v,w;
};
vector<Node> e;
int dist[55];
int main(){
    int m;
    while(cin>>m)
	{
        int s,t;
		cin>>s>>t;
        e.clear();
        memset(dist,0x3f,sizeof dist);
        dist[s]=0;
        int a,b,c;
        while(cin>>a and a!=0){
            cin>>b>>c;
            e.push_back({a,b,c});
            e.push_back({b,a,c}); 
        }
        for(int i=1;i<=m-1;i++){
            bool update=0;
            for(auto &ed:e)
			{
                int u=ed.u,v=ed.v,w=ed.w;
                if(dist[v]>dist[u]+w)
				{
                    dist[v]=dist[u]+w;
                    update=1;
                }
            }
            if(!update) 
			{
				break; 
			}
        }
        cout<<dist[t]<<'\n';
    }
    return 0;
}

AC记录

SPFA
C++
#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
vector<pair<int,int>> g[55];
int dist[55],inq[55];
int main(){
    int m;
    while(cin>>m){
        int s,t;cin>>s>>t;
        for(int i=1;i<=m;i++) 
		{
			g[i].clear();
		}
        memset(dist,0x3f,sizeof dist);
        memset(inq,0,sizeof inq);
        int a,b,c;
        while(cin>>a and a!=0){
            cin>>b>>c;
            g[a].emplace_back(b,c);
            g[b].emplace_back(a,c);
        }
        queue<int> q;
        dist[s]=0; q.push(s); 
		inq[s]=1;
        while(!q.empty()){
            int u=q.front(); 
			q.pop(); 
			inq[u]=0;
            for(auto [v,w]:g[u]){
                if(dist[v]>dist[u]+w)
				{
                    dist[v]=dist[u]+w;
                    if(!inq[v])
					{
                        q.push(v);
                        inq[v]=1;
                    }
                }
            }
        }
        cout<<dist[t]<<'\n';
    }
    return 0;
}

AC记录

算法时间复杂度适用场景本题适配度
FloydO(m3)O(m^3)多组查询、节点极少★★★★★(m=50,(50^3=125000)完全无压力)
堆优化 DijkstraO(Elongm)O(Elongm)单源最短路、稠密 / 稀疏图通用★★★★★(标准最优)
SPFA平均O(E)O(E)带负权、稀疏图★★★★☆
Bellman-FordO(ME)O(M E)教学、负环判定★★☆☆☆(本题没必要)
原文