地铁修建(csp201703-4)(kruskal || Dijkstra)

问题描述

\hspace{17pt} A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市决定在1号到n号枢纽间修建一条地铁。
  地铁由很多段隧道组成,每段隧道连接两个交通枢纽。经过勘探,有m段隧道作为候选,两个交通枢纽之间最多只有一条候选的隧道,没有隧道两端连接着同一个交通枢纽。
  现在有n家隧道施工的公司,每段候选的隧道只能由一个公司施工,每家公司施工需要的天数一致。而每家公司最多只能修建一条候选隧道。所有公司同时开始施工。
  作为项目负责人,你获得了候选隧道的信息,现在你可以按自己的想法选择一部分隧道进行施工,请问修建整条地铁最少需要多少天。

Input

\hspace{17pt} 输入的第一行包含两个整数n, m,用一个空格分隔,分别表示交通枢纽的数量和候选隧道的数量。
  第2行到第m+1行,每行包含三个整数a, b, c,表示枢纽a和枢纽b之间可以修建一条隧道,需要的时间为c天。

Output

输出一个整数,修建整条地铁线路最少需要的天数。

Sample input

6 6
1 2 4
2 3 4
3 6 7
1 4 2
4 5 5
5 6 6

Sample output

6

样例说明

可以修建的线路有两种。
第一种经过的枢纽依次为1, 2, 3, 6,所需要的时间分别是4, 4, 7,则整条地铁线需要7天修完;
第二种经过的枢纽依次为1, 4, 5, 6,所需要的时间分别是2, 5, 6,则整条地铁线需要6天修完。
第二种方案所用的天数更少。

数据范围

\hspace{17pt} 对于20%的评测用例,1 ≤ n ≤ 10,1 ≤ m ≤ 20;
  对于40%的评测用例,1 ≤ n ≤ 100,1 ≤ m ≤ 1000;
  对于60%的评测用例,1 ≤ n ≤ 1000,1 ≤ m ≤ 10000,1 ≤ c ≤ 1000;
  对于80%的评测用例,1 ≤ n ≤ 10000,1 ≤ m ≤ 100000;
  对于100%的评测用例,1 ≤ n ≤ 100000,1 ≤ m ≤ 200000,1 ≤ a, b ≤ n,1 ≤ c ≤ 1000000。

所有评测用例保证在所有候选隧道都修通时1号枢纽可以通过隧道到达其他所有枢纽。

题意

题干看起来相当复杂,实际上主要问题就是使得1号点和n号点联通,选择代价是若干边权中的最大值,为选择代价最小是多少。

思路

三种方法可以解这道题,1.二分答案。2.kruskal。3.dijkstra。

这里讲解后两种思路。

kruskal做法

这个题最终要求1号点和n号点连接,因此我们可以使用并查集,而要选择代价最小,我们使用kruskal依次加边,直到1号和n号连接在一起结束,输出最后一次加点边。类似于最小瓶颈生成树?和csp201812-4代码只有一行不同。

完整代码-kruskal

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

const int maxn=100000+10;
struct node
{
    int x,y,w;
};
node a[maxn*2];
int par[maxn],rnk[maxn],ans;
void init(int n)
{
    for (int i=0; i<=n; i++)
    {
        par[i]=i;
        rnk[i]=1;
    }
}
int find(int x)
{
    return par[x]==x ? x : par[x]=find(par[x]);
}
bool unite(int x,int y)
{
    x=find(x); y=find(y);
    if(x==y) return false;

    if(rnk[x]>rnk[y]) swap(x,y);
    par[x]=y; rnk[y]+=rnk[x]; rnk[x]=rnk[y];
    return true;
}
int getint()
{
    int x=0,s=1;
    char ch=' ';
    while(ch<'0' || ch>'9')
    {
        ch=getchar();
        if(ch=='-') s=-1;
    }
    while(ch>='0' && ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*s;
}
bool comp(node a,node b)
{
    return a.w<b.w;
}
int main()
{
    int n=getint(),m=getint();

    for (int i=1; i<=m; i++)
    {
        a[i].x=getint(); a[i].y=getint(); a[i].w=getint();
    }
    sort(a+1,a+1+m,comp);

    int tot=0;
    init(n);
    for (int i=1; i<=m; i++)
    {
        if(unite(a[i].x,a[i].y))
        {
            tot++; ans=a[i].w;
        }
        if(find(1)==find(n) || tot==n-1) break;
    }
    printf("%d\n",ans);
    return 0;
}

dijkstra做法

最短路算法的松弛条件是路径之和最小,而这个题是要路径上的最大边权最小,因此我们只需要更改一个松弛条件即可,将以前的dis[y]>dis[x]+w改成dis[y]>max(dis[x],w)

完整代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;

const int maxn=200000+10;
struct node
{
    int to,w,next;
};
node edge[maxn*2];
int head[maxn],dis[maxn];
int cnt,n,m;
bool visit[maxn];

void add(int x,int to,int w)
{
    cnt++;
    edge[cnt].to=to; edge[cnt].w=w;
    edge[cnt].next=head[x];
    head[x]=cnt;
}
void dijkstra(int s1)
{
    memset(dis,0x3f,sizeof(dis));
    dis[s1]=0;

    priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > points;
    //代码返回pair的比较结果,先按照pair的first元素升序,first元素相等时,再按照second元素升序:
    points.push(make_pair(0,s1));//第一个参数指从s到第二个参数到距离

    while(!points.empty())
    {
        int x=points.top().second;
        points.pop();
        if(!visit[x])
        {
            visit[x]=true;
            for(int i=head[x];i;i=edge[i].next)
            {
                int to=edge[i].to;
                if(dis[to]>max(dis[x],edge[i].w))
                {
                    dis[to]=max(dis[x],edge[i].w);
                    points.push(make_pair(dis[to],to));
                }
            }
        }
    }
}
int main()
{
    cin>>n>>m;
    for (int i=1; i<=m; i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        add(x,y,z);
        add(y,x,z);
    }

    dijkstra(1);

    cout<<dis[n]<<endl;
    return 0;
}
发布了32 篇原创文章 · 获赞 24 · 访问量 2216

猜你喜欢

转载自blog.csdn.net/weixin_43347376/article/details/105255840