zoj 2966 Build The Electric System 最小生成树

Escape Time II

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2966

Description

In last winter, there was a big snow storm in South China. The electric system was damaged seriously. Lots of power lines were broken and lots of villages lost contact with the main power grid. The government wants to reconstruct the electric system as soon as possible. So, as a professional programmer, you are asked to write a program to calculate the minimum cost to reconstruct the power lines to make sure there‘s at least one way between every two villages.

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases. And it will be followed by T consecutive test cases.

In each test case, the first line contains two positive integers N and E (2 <= N <= 500, N <= E <= N * (N - 1) / 2), representing the number of the villages and the number of the original power lines between villages. There follow E lines, and each of them contains three integers, A, B, K (0 <= A, B < N, 0 <= K < 1000). A and B respectively means the index of the starting village and ending village of the power line. If K is 0, it means this line still works fine after the snow storm. If K is a positive integer, it means this line will cost K to reconstruct. There will be at most one line between any two villages, and there will not be any line from one village to itself.

Output

For each test case in the input, there‘s only one line that contains the minimum cost to recover the electric system to make sure that there‘s at least one way between every two villages.

Sample Input

1
3 3
0 1 5
0 2 0
1 2 9

Sample Output

5

HINT

题意

给你个无向边带权图,让你输出最小生成树

题解:

Kruskal or prim

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
    ll 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;
}
inline void P(int x)
{
    Num=0;if(!x){putchar(‘0‘);puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

int u[maxn],v[maxn],w[maxn],r[maxn];
int pa[maxn],n,m;
bool cmp(int i,int j)
{
    return w[i]<w[j];
}
int fi(int x)
{
    return pa[x]==x?x:pa[x]=fi(pa[x]);
}
int kruskal()
{
    int ans=0;
    for(int i=0;i<n;i++)
        pa[i]=i;
    for(int i=0;i<m;i++)
        r[i]=i;
    sort(r,r+m,cmp);
    for(int i=0;i<m;i++)
    {
        int e=r[i];
        int x=fi(u[e]);
        int y=fi(v[e]);
        if(x!=y)
            ans+=w[e],pa[x]=y;
    }
    return ans;
}
int main()
{
    //test;
    int t=read();
    while(t--)
    {
        n=read(),m=read();
        for(int i=0;i<m;i++)
        {
            int a=read(),b=read(),c=read();
            u[i]=a,v[i]=b,w[i]=c;
        }
        printf("%d\n",kruskal());
    }
}
时间: 2024-10-13 14:05:15

zoj 2966 Build The Electric System 最小生成树的相关文章

HDU 4081 Qin Shi Huang&#39;s National Road System(最小生成树+暴力枚举边)

题目大意:给你1000个点,每个点上有一个数目代表这个城市有多少人,让你把这N个点构成一颗生成树,你可以删除其中的任意一条边.让你求出一个比例A/B是的这个比例最大,A表示你删除那条边上两个城市的人口数之和,B表示的是去掉这条变这可生成树上其他的边的总长度. 解体思路:先求出来最小生成树,然后暴力枚举生成树的边,B=总数-这条边的长度.A = 将这条连断开之后左右集合中权值最大的两个数的和. 这样保证了B最小的情况下,去找最大的A,所以是可行的解.生成树的同时建边,然后dfs找最大值. PS:这

HDU 4081 Qin Shi Huang&#39;s National Road System 最小生成树+倍增求LCA

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5428    Accepted Submission(s): 1902 Problem Description

Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG]

Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG] <ignore_js_op> 程序员文本编辑器 Sublime Text 3 Build 3065 全平台破解版,windows 32 bit or 64 bit.Ubuntu 32 bit or 64 bit.OS X全部支持. Sublime Text 是一个轻量.简洁.高效.跨平台的编辑器.Sublime Text 的特色功能:良好的扩展功能,官方称之为安装包(Packa

HDU 4081 Qin Shi Huang&#39;s National Road System 最小生成树

分析:http://www.cnblogs.com/wally/archive/2013/02/04/2892194.html 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成树然后查询就好了 然后预处理那个数组是O(n^2)的,这样总时间复杂度是O(n^2+m) 这是因为这个题n比较小,如果n大的时候,就需要路径查询了,比如LCA 或者树链剖分达到O(mlogn) #include <iostream> #include <algorithm> #incl

HDU 4081 Qin Shi Huang&#39;s National Road System(最小生成树/次小生成树)

题目链接:传送门 题意: 有n坐城市,知道每坐城市的坐标和人口.现在要在所有城市之间修路,保证每个城市都能相连,并且保证A/B 最大,所有路径的花费和最小,A是某条路i两端城市人口的和,B表示除路i以外所有路的花费的和(路径i的花费为0). 分析: 先求一棵最小生成树,然后枚举每一条最小生成树上的边,删掉后变成两个生成树,然后找两个集合中点权最大的两 个连接起来.这两个点中必然有权值最大的那个点,所以直接从权值最大的点开始dfs. 为了使A/B的值最大,则A尽可能大,B尽可能小.所以B中的边一定

ZOJ 1639 Hang Up the System (状态压缩)

Hang Up the System Time Limit: 2 Seconds      Memory Limit: 32768 KB You're going to design a multi-task operating system for an embedded system. Because the resources are limited on this mini computer, parallel running of some programs will probably

ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

题目连接:ZOJ 1542 POJ 1861 Network 网络 Network Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, t

POJ 2031 &amp;&amp; ZOJ 1718--Building a Space Station【最小生成树 &amp;&amp; kurskal &amp;&amp; 水题】

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5655   Accepted: 2848 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar

ZOJ 3396 Conference Call(3点最小生成树)

题意:给出一组含m个点的无向图,再给出n个点,这n个点分别以一条边连接到这个无向图中的某个点.对于每个询问,求出3点连通的最小代价.有可能3个点是不能互通的.如图,最小代价就是红色的边的权之和. 思路:先对m个点的无向图进行求两两之间最短路径,用floyd.接下来对于每个询问,穷举m个点,求3个点分别到该点的距离之和,求最小即可. 1 //#include <bits/stdc++.h> 2 #include <iostream> 3 #include <cstdio>