图论 --- 最小生成树 + 剪枝 + 路径合并

简介: Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9941    Accepted Submissio...

Connect the Cities

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9941    Accepted Submission(s): 2827


Problem Description
In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don’t want to take too much money.  
 

 

Input
The first line contains the number of test cases.
Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.
 

 

Output
For each case, output the least money you need to take, if it’s impossible, just output -1.
 

 

Sample Input
1 6 4 3 1 4 2 2 6 1 2 3 5 3 4 33 2 1 2 2 1 3 3 4 5 6

 


 

Mean:

好久没刷水题了。。。

给你一个有向图,其中某些边是连接的,现在要你找一棵最小生成树。

 

analyse:

先用并查集来合并已经连通的边,然后在使用kruskal来求最小生成树,注意这儿有一个剪枝,因为这题的边很多,所以我们不需要全部判断,根据最小生成树的性质可知,我们只需要找出n-1条边即可。

 

Time complexity:O(n)

 

Source code:

 

//Memory   Time
// 5644K   241MS
//by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAXV 520
#define MAXE 25010
#define LL long long
using namespace std;
int T,n,m,k;
struct Node
{
    int s,e,val;
} graph[MAXE];
int father[MAXV];

void scan(int& x){
        x = 0;
        char c = getchar ();
        while (!(c>='0' && c<='9' || c=='-'))     c = getchar ();
        while (c >= '0' && c <= '9'){
                x = x * 10 + c - '0';
                c = getchar ();
        }
}

int Find(int x)
{
    return x==father[x]?x:father[x]=Find(father[x]);
}

bool cmp(Node a,Node b)
{
    return a.val<b.val;
}
int kruskal()
{
    int ans=0;
    int cnt=0;
    sort(graph+1,graph+1+m,cmp);
    for(int i=1;i<=n;i++)
        if(father[i]==i)
            cnt++;
    for(int i=1;i<=m&&cnt>1;i++)
    {
        int x=Find(graph[i].s);
        int y=Find(graph[i].e);
        if(x!=y)
        {
            father[x]=y;
            ans+=graph[i].val;
            cnt--;
        }
    }
    if(cnt==1)
        return ans;
    else return -1;
}


int main()
{
//    freopen("cin.txt","r",stdin);
//    freopen("cout.txt","w",stdout);
    scan(T);
    while(T--)
    {
        scan(n);
        scan(m);
        scan(k);
        for(int i=1;i<=m;i++)
        {
            scan(graph[i].s);
            scan(graph[i].e);
            scan(graph[i].val);
        }
        for(int i=0;i<=n;i++)
            father[i]=i;
        int cnt,fa,son;
        while(k--)
        {
            scan(cnt);
            scan(fa);
            fa=Find(fa);
            while(--cnt)
            {
                scan(son);
                father[Find(son)]=fa;
            }
        }
        int mincost=kruskal();
        printf("%d\n",mincost);
    }
    return 0;
}

  

 

目录
相关文章
|
4月前
|
算法
Hierholzer算法dfs找欧拉回路模板
Hierholzer算法dfs找欧拉回路模板
38 0
|
19天前
|
存储 算法
图的深度优先算法
图的深度优先算法
19 0
|
1月前
|
机器学习/深度学习 算法 测试技术
【图轮】【 最小生成树】【 并集查找】1489. 找到最小生成树里的关键边和伪关键边
【图轮】【 最小生成树】【 并集查找】1489. 找到最小生成树里的关键边和伪关键边
|
3月前
|
测试技术
【树】【图论】【树路径】【深度优先搜索】2867. 统计树中的合法路径数目
【树】【图论】【树路径】【深度优先搜索】2867. 统计树中的合法路径数目
|
10月前
|
存储 索引
树与图中的dfs和bfs—— AcWing 846. 树的重心 AcWing 847. 图中点的层次
树与图中的dfs和bfs—— AcWing 846. 树的重心 AcWing 847. 图中点的层次
49 0
|
10月前
|
Cloud Native 算法 Go
886. 可能的二分法:图+深度优先算法
这是 力扣上的 886. 可能的二分法,难度为 中等。
|
11月前
|
算法
转:一个极简的Dijkstra算法示例
Dijkstra算法是一种用于计算一个起点到其他所有点的最短路径的算法。它是贪心算法的一种,基于贪心策略,用来找单源最短路径问题。该算法常用于路由算法和作为其他图算法的一个子模块。 Dijkstra算法的时间复杂度为O(E + VlogV)。
48 0
转:一个极简的Dijkstra算法示例
|
12月前
|
存储
搜索与图论-有向图的拓扑序列
搜索与图论-有向图的拓扑序列
|
算法
回溯法——面试题矩阵中的路径(一)
回溯法介绍 回溯法应用(实例化) 回溯法介绍 1.1 回溯法是蛮力法的升级版,它从解决问题每一步的所有可能选项里系统的选择一个可行的解决方案。 1.2 回溯法非常适合由多个步骤组成的问题,并且每个步骤都有多个选项。当我们在某一步做出一个选择时,就进到下一步了,如果不符合题目条件,就回溯到原来的步骤进行新的选择,如果这步的选择还没有符合条件的直到选到符合题目条件的选项为止。如果都不符合的话,就会再回溯到上一步,然后进入再进行新的选择,就这样重复选择,直到到达最终的状态。 1.3 通常回溯法算法适合用递归实现代码,当我们到达某一个节点时,尝试所有可能的选项并在满足条件的前提下递归地抵达下一个节点。
66 0
|
存储
【每日一题Day61】寻找图中是否存在路径 | BFS 并查集
初始时将source设为已访问,并且将其加入队列。之后每次将队列中的节点出队,并将其能够到达的未访问过的节点入队。如果访问到destination,则直接返回true;如果队列为空,仍未访问到destination,则返回false
91 0