LightOJ 1123 Trail Maintenance

题意:n个城市m天、每一天修一条道路,输出当前天数的最小生成树,但是这里有一个条件,就是说最小生成树必须包括全部n个城市,否则输出-1

思路:边数有6000如果每一天跑一次最小生成树的话就接近O(m^2logm)再加上数据量、是很可能会超时的、这时我们就得想想能不能删去一些边、这样在求最小生成树的时候就可以减少很多不必要的边,因为只有n(<=200)个城市,最多只会有n-1条边,那么哪种情况下的边可以删去呢?

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 const int qq=7000;
 7 int pre[500];
 8 struct Edge
 9 {
10     int u,v,w;
11     bool operator < (const Edge a)const{
12         return w<a.w;
13     }
14 }edge[qq];
15 int cnt;
16 int find(int x)
17 {
18     if(x==pre[x])    return x;
19     return pre[x]=find(pre[x]);
20 }
21 int kruscal(int n)
22 {
23     for(int i=1;i<=n;++i)
24         pre[i]=i;
25     int u,v,w;
26     int minx=0,count=1;
27     int ans=cnt;    //先记录当前所有的边数、
28     for(int i=0;i<ans;++i){        //遍历所有的边、
29         u=edge[i].u;v=edge[i].v;w=edge[i].w;
30         int x=find(u);
31         int y=find(v);
32         if(x==y){        //这种情况下是一定会删边的、
33             edge[i]=edge[cnt-1];
34             --cnt;        //因为开始已经将当前边数数值给ans了,所以不用担心遍历不到当前的所有边
35             continue;    // 可以直接进行删边操作了、
36         }
37         ++count;        //记录当前的边数、
38         pre[y]=x;
39         minx+=w;
40     }
41     if(count==n)    return minx;    //因为是开始count的初值是1,所以只需要判断当前边数等于n与否
42     else    return -1;
43 }
44 int main()
45 {
46     int t;scanf("%d",&t);
47     int k=1;
48     while(t--){
49         int n,m;
50         scanf("%d%d",&n,&m);
51         int u,v,w;
52         cnt=0;
53         printf("Case %d:\n",k++);
54         for(int i=0;i<m;++i){
55             scanf("%d%d%d",&u,&v,&w);
56             edge[cnt].u=u;edge[cnt].v=v;edge[cnt++].w=w;
57             sort(edge,edge+cnt);
58             printf("%d\n",kruscal(n));
59         }
60     }
61     return 0;
62 }

在结构体内重载运算符的时候记得加return 记得加return 记得加return

重要事情说三遍、!!!

时间: 2024-08-01 16:00:45

LightOJ 1123 Trail Maintenance的相关文章

LightOj 1123-Trail Maintenance(最小生成树:神级删边)

1123 - Trail Maintenance PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Tigers in the Sunderbans wish to travel freely among the N fields (numbered from 1 to N), even though they are separated by trees. The tigers wish to

HUTACM2016 MST练习&#183;解题报告

专题链接 A - 还是畅通工程 题解: n个村,m条路,要用最少的钱把所有村连接起来,MST的模板题,提供两种算法模板. //使用Kruskal算法 #include<stdio.h> #include<algorithm> #include<string.h> using namespace std; const int N = 105; int seed[N]; //构建并查集 int find_root(int x){ return seed[x] < 0?

Auditing Enhancements (Audit Policies and Unified Audit Trail) in Oracle Database 12c Release 1 (12.1)

select substrb(parameter_name ,1,25) name, substrb(parameter_value,1,20) value, substrb(audit_trail ,1,20) trail from dba_audit_mgmt_config_params ; 8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux Home » Art

LightOJ 1030 Discovering Gold【概率】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题意:基础概率题. 代码: #include <stdio.h> #include <string.h> #include <vector> #include <string> #include <algorithm> #include <iostream> #include <iterator>

Magento database maintenance

OverviewThis article examines the various ways to maintain an efficient Magento database, even when large in size. Magento does many things well, but maintaining an efficient database is not one of them. Having many products is a good reason to have

LightOJ - 1370 Bi-shoe and Phi-shoe

题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1370 题目大意:给N个数a[i], N <= 1e6,问使 Φ(x) >= a[i] 成立的最小x的所有x的和是多少. 解题思路:我们知道的是对于素数 m 来说,phi[m] = m - 1.另一方面,对于一个合数 m 来说, phi[m] < phi[x] , x > m && x 是素数. 因此,我们可以认为,每

linux开机出现一下错误Give root password for maintenance (or type Control-D to continue):

linux开机出现一下错误Give root password for maintenance (or type Control-D to continue): 第一种错误的情况: 由于错误的编辑/etc/fstab文件 而引起的不能正常进入系统.假如你将某一个分区或者磁盘最后一个参数设置为1或2时,系统默认会在开机过程中检查这个磁盘的扇区.假如系统检查不到这个磁盘,或者这个磁盘根本检测不到(尤其是在网络存储时)那么系统就会报错,导致出现这种情况. 解决办法:输入root密码,此时整个文件系统是

lightoj 1057 - Collecting Gold(状压dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1057 题解:看似有点下记忆话搜索但是由于他是能走8个方向的也就是说两点的距离其实就是最大的x轴或y轴的差.然后只有15个藏金点状压一下加dfs就行了. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define inf 0X3f3f3f

LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】

1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. F