【Spfa/最短路模板】遍历所有点的最短路径

题目描述

明明暑假来济南旅游旅游,他打算游玩N个旅游景点,N-1条双向连接的道路将它们联通起来,每一条道路有固定长度。一开始明明位于1号景点。
现在希望你能够求出旅行长度最小的方案,使得每个景点至少被访问到一次。

输入

第一行两个整数N,代表景点数目。
接下来N-1行,每行三个整数s, t, w,表示有一条从s到t的双向道路,长度为w。s和t的编号从1开始。

输出

一行一个整数,代表能够访问每个景点至少一次的方案的最小旅行长度。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=50005;
struct Edge
{
    int v;
    int cost;
    Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<Edge> E[maxn];
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}
bool vis[maxn];
int cnt[maxn];
int dist[maxn];
bool spfa(int st,int n)
{
    memset(vis,false,sizeof vis);
    for(int i=1;i<=n;i++)
        dist[i]=INF;
    vis[st]=true;
    dist[st]=0;
    queue<int>qu;
    while(!qu.empty())
    {
        qu.pop();
    }
    qu.push(st);
    memset(cnt,0,sizeof cnt);
    cnt[st]=1;
    while(!qu.empty())
    {
        int u=qu.front();
        qu.pop();
        vis[u]=false;
        for(int i=0;i<E[u].size();i++)
        {
            int v=E[u][i].v;
            if(dist[v]>dist[u]+E[u][i].cost)
            {
                dist[v]=dist[u]+E[u][i].cost;
                if(!vis[v])
                {
                    vis[v]=true;
                    qu.push(v);
                    if(++cnt[v]>n)
                        return false;
                }
            }
        }
    }
    return true;
}
int vis0[50005];
int main()
{
    int n;
    scanf("%d",&n);
    int u, v, w;
    int ant=0,mx=0;
    for (int i = 1; i <= n-1; i++)
    {
        scanf("%d %d %d",&u,&v,&w);
        ant+=w;
        addedge(u,v,w);
        addedge(v,u,w);
        vis0[u]=0;
        vis0[v]=1;
    }
    ant*=2;
    spfa(1,n);
    for(int i=1;i<=n;i++)
    {
        mx=max(mx,dist[i]);
    }
    printf("%d\n",ant-mx);
    return 0;
}

原文地址:https://www.cnblogs.com/Diliiiii/p/10305755.html

时间: 2024-08-03 16:46:36

【Spfa/最短路模板】遍历所有点的最短路径的相关文章

POJ 2449Remmarguts&#39; Date K短路模板 A*+SPFA

太水了我不想说了,模板在这里 14312K 313MS 1 #include<queue> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int v[100010],v2[100010],c[100010],c2[100010],s,t,k,duin; 7 int n,m,point[1010],next[100010],cnt=0,

[poj2449]Remmarguts&#39; Date(K短路模板题,A*算法)

解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<cmath> #include<queue> using namespace std; typedef long long ll; const int N=1e3+10; const

poj1511/zoj2008 Invitation Cards(最短路模板题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds      Memory Limit: 65536 KB In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fa

poj 2449 Remmarguts&#39; Date (k短路模板)

Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30772   Accepted: 8397 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly tou

HDU 5521.Meeting 最短路模板题

Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3361    Accepted Submission(s): 1073 Problem Description Bessie and her friend Elsie decide to have a meeting. However, after Farmer Jo

uva 11374 最短路+记录路径 dijkstra最短路模板

UVA - 11374 Airport Express Time Limit:1000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu [Submit]  [Go Back]  [Status] Description ProblemD: Airport Express In a small city called Iokh, a train service, Airport-Express, takes residents to t

hdu 4568 spfa 最短路算法+旅行商问题

http://acm.hdu.edu.cn/showproblem.php?pid=4568 Problem Description One day, a hunter named James went to a mysterious area to find the treasures. James wanted to research the area and brought all treasures that he could. The area can be represented a

POJ 2449 Remmarguts&#39; Date(k短路模板)

link:https://vjudge.net/problem/POJ-2449 前面输入与大多最短路题相同 最后一行输入s,t,k 求从s到t的第K短路 #include <iostream> #include <cstring> #include <queue> using namespace std; const int MAXN=1010; struct node { int p,g,h; bool operator < (node a) const {

HDU 2544 最短路 SPFA 邻接表 模板

Problem Description 在每年的校赛里,全部进入决赛的同学都会获得一件非常美丽的t-shirt.可是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以如今他们想要寻找最短的从商店到赛场的路线,你能够帮助他们吗? Input 输入包含多组数据.每组数据第一行是两个整数N.M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路.N=M=0表示输入结束.接下来M行,每行