LightOj 1123-Trail Maintenance(最小生成树:神级删边)
1123 - Trail Maintenance
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 maintain trails between pairs of fields so that they
can travel from any field to any other field using the maintained trails. Tigers may travel along a maintained trail in either direction.
The tigers do not build trails. Instead, they maintain deer trails that they have discovered. On any week, they can choose to maintain any or all of the deer animal trails they know about. Always curious, the tigers discover one new deer trail at the beginning
of each week. They must then decide the set of trails to maintain for that week so that they can travel from any field to any other field. Tigers can only use trails which they are currently maintaining.
The tigers always want to minimize the total length of trail they must maintain. The tigers can choose to maintain any subset of the deer trails they know about, regardless of which trails were maintained the previous week. Deer trails (even when maintained)
are never straight. Two trails that connect the same two fields might have different lengths. While two trails might cross, tigers are so focused; they refuse to switch trails except when they are in a field. At the beginning of each week, the tigers will
describe the deer trail they discovered. Your program must then output the minimum total length of trail the tigers must maintain that week so that they can travel from any field to any other field, if there is such a set of trails.
Input
Input starts with an integer T (≤ 25), denoting the number of test cases.
Each case starts with two integers N (1 ≤ N ≤ 200) and W. W is the number of weeks the program will cover (1 ≤ W ≤ 6000).
Each of the next W lines will contain three integers describing the trail the tigers found that week. The first two numbers denote the end points (filed numbers) and the third number denotes the length of the trail (1 to 10000).
No trail has the same field as both of its end points.
Output
For each case, print the case number in a line. Then for every week, output a single line with the minimum total length of trail the tigers must maintain so that they can travel from any field to any other field. If no set of trails allows the tigers to
travel from any field to any other field, output "-1".
Sample Input |
Output for Sample Input |
1 4 6 1 2 10 1 3 8 3 2 3 1 4 3 1 3 6 2 1 2 |
Case 1: -1 -1 -1 14 12 8 |
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <list>
using namespace std;
const int maxn=5000;
const int INF=1<<25;
int n,m,u[maxn],v[maxn],w[maxn],eg[maxn],fa[maxn];
void Make_set()
{
for(int i=0;i<=n;i++)
fa[i]=i;
}
int Find(int x)
{
if(x==fa[x]) return x;
return fa[x]=Find(fa[x]);
}
bool cmp(int a,int b)
{
return w[a]<w[b];
}
int num;
int Kru()
{
int d=-1,i,ans=0,cnt=0;
Make_set();
for(i=0;i<num;i++)
eg[i]=i;
sort(eg,eg+num,cmp);
for(i=0;i<num;i++)
{
int e=eg[i];
int fx=Find(u[e]);
int fy=Find(v[e]);
if(fx==fy)
{
d=e;
continue;
}
fa[fx]=fy;
ans+=w[e];
cnt++;
}
if(d!=-1)//删边
{
num--;
u[d]=u[num];
v[d]=v[num];
w[d]=w[num];
}
if(cnt==n-1)
return ans;
else
return -1;
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
printf("Case %d:\n",cas++);
num=0;//边数
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u[num],&v[num],&w[num]);
num++;
printf("%d\n",Kru());
}
}
return 0;
}
用结构体写了一下。还有要注意就是这题特别卡cin cout
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <list>
using namespace std;
const int maxn=500001;
const int INF=1<<29;
struct node{
int u,v,w;
friend bool operator<(node a,node b){
return a.w<b.w;
}
};
node eg[maxn];
int n,m,fa[maxn];
void Make_set()
{
for(int i=1;i<=n;i++)
fa[i]=i;
}
int Find(int x)
{
if(x!=fa[x])
fa[x]=Find(fa[x]);
return fa[x];
}
int num;
int Kru()
{
Make_set();
sort(eg,eg+num);
int ans=0,cnt=0,d=-1;
for(int i=0;i<num;i++)
{
int fx=Find(eg[i].u);
int fy=Find(eg[i].v);
if(fx==fy)
{
d=i;
continue;
}
else
{
fa[fx]=fy;
ans+=eg[i].w;
cnt++;
}
}
if(d!=-1) eg[d]=eg[--num];
if(cnt==n-1) return ans;
return -1;
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
printf("Case %d:\n",cas++);
num=0;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&eg[num].u,&eg[num].v,&eg[num].w);
num++;
printf("%d\n",Kru());
}
}
return 0;
}
LightOj 1123-Trail Maintenance(最小生成树:神级删边)的更多相关文章
- LightOJ 1123 Trail Maintenance
题意:n个城市m天.每一天修一条道路,输出当前天数的最小生成树,但是这里有一个条件,就是说最小生成树必须包括全部n个城市,否则输出-1 思路:边数有6000如果每一天跑一次最小生成树的话就接近O(m^ ...
- VIM自动补全插件 - YouCompleteMe--"大神级vim补全插件"
VIM自动补全插件 - YouCompleteMe 序言 vim 之所以被称为编辑器之神多半归功于其丰富的可DIY的灵活插件功能,( 例如vim下的这款神级般的代码补全插件YouCompleteMe) ...
- Github欢乐多 PHP神级代码引发吐槽热
前日,github的PHP板块惊现一段能够提升70%运行效率的代码,引发了全世界众多网友的吐槽和调侃,“awesome!”.“well done!”.“PHP是世界第一语言!”平时不苟言笑,埋头苦干的 ...
- 用了TextMate才知道什么叫神级Editor
用了TextMate才知道什么叫神级Editor 一直用Eclipse作为开发Ruby和Java项目的IDE,但是太耗内存,再开个Firefox和虚拟机就可以直接将MBP弄残了..看到大家都对Mac下 ...
- 20171201 - macOS High Sierra 神级 bug
昨日亲测有效,macOS High Sierra 神级 bug,系统管理员 root 密码为空,输入就可以登录,具备最高权限. 让人不禁想象 Apple Software 怎么了,人才都流失了吗?
- 《程序员代码面试指南》第三章 二叉树问题 遍历二叉树的神级方法 morris
题目 遍历二叉树的神级方法 morris java代码 package com.lizhouwei.chapter3; /** * @Description:遍历二叉树的神级方法 morris * @ ...
- 大神级回答exists与in的区别
google搜了一下,很多帖子,而且出发点不同,各有各的道理,但是有一个帖子讲的特别好: http://zhidao.baidu.com/question/134174568.html 忍不住在百度上 ...
- IntelliJ IDEA 15款 神级超级牛逼插件推荐(超赞,谁用谁知道)
满满的都是干货 所有插件都是在 ctrl+alt+s 里的plugins 里进行搜索安装 1.CodeGlance 代码迷你缩放图插件 2. Codota 代码提示工具,扫描你的代码后,根据你的敲击 ...
- mac上的键盘生活——神级输入法:鼠须管
好吧,我是今天才知道这个无敌的输入法有多么强悍,传说中的神级输入法,鼠须管~ 在这之前我都一直是用的搜狗输入法,因为以前在win下就一直都用的是搜狗输入法,怎么说,各种国产的输入法做的还是比较有良心的 ...
随机推荐
- C/C++ 函数指针 总结
什么是函数指针 就像某一变量的地址可以存储在相应的指针变量中一样,指向函数的指针中保存着函数代码起始处的地址 函数指针的声明 当声明一个函数指针时,必须声明它指向的函数类型.要指定函数类型,就要指出函 ...
- Delphi7下使用FastMM4
1> 将文件Replacement BorlndMM DLL/Precompiled/for Delphi IDE/Performance/BorlndMM.dll,替换掉Delphi/Bin下 ...
- WIN8.1 IE11 F12开发者人员工具,开启什么都不显示
WIN8.1 IE11 F12开发者人员工具,开启什么都不显示,求大神帮忙? IE11也没法重装, 还原IE设置过了. 没有效果. 也试过把IE停掉,重启后再重新打开 也没用 已经解决了 删除 ...
- MVC文件上传03-使用Request.Files上传多个文件
本篇体验在控制器方法中使用controllerContext.HttpContext.Request.Files上传多个文件.兄弟篇为: MVC文件上传01-使用jquery异步上传并客户端验证类型和 ...
- 求好用的在线手册编写工具,能编写像bootstrap在线Doc那种的,想为OpenCenter写个在线手册
原文地址:http://www.oschina.net/question/1014458_225711 各位OSCer的开源项目的在线说明文档都是用什么写的呢? 类似 http://v3.bootcs ...
- 防火墙 0x80070422
1.无法打开操作中心-安全服务,解决方法:控制面板->管理工具->服务,找到Security Center 服务,双击打开,查看启动类型是否设置成禁用,是的话更改成自动或者延迟启动,之后就 ...
- UCOS移植心得(
移植UCOS之前,你首先应该做好三件事: 1.弄懂UCOS,这是谁都知道的哦 ^_^ 2. 弄懂你想要移植到的硬件平台 3. 清楚你使用的编译器是如何处理函数的局部变量和怎么样处理函数间的参数传递 这 ...
- [转]C++之运算符重载(2)
上一节主要讲解了C++里运算符重载函数,在看了单目运算符(++)重载的示例后,也许有些朋友会问这样的问题.++自增运算符在C或C++中既可以放在操作数之前,也可以放在操作数之后,但是前置和后置的作用又 ...
- Visual Studio 2015官方社区版/专业版/专业版下载地址
Visual Studio 2015官方社区版/专业版/专业版下载地址 以下 Visual Studio 2015 社区版/专业版/专业版资源都是官方MSDN原版下载资源,统一为ISO格式镜像,使用解 ...
- Pytoch 抽取中间层特征方法
定义一个特征提取的类: 参考pytorch论坛:How to extract features of an image from a trained model from torchvision.mo ...