Matrix

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 1
Problem Description
Machines have once again attacked the kingdom of Xions. The kingdom of Xions has N cities and N-1 bidirectional roads. The road network is such that there is a
unique path between any pair of cities.

Morpheus has the news that K Machines are planning to destroy the whole kingdom. These Machines are initially living in K different cities of the kingdom and
anytime from now they can plan and launch an attack. So he has asked Neo to destroy some of the roads to disrupt the connection among Machines. i.e after destroying those roads there should not be any path between any two Machines.

Since the attack can be at any time from now, Neo has to do this task as fast as possible. Each road in the kingdom takes certain time to get destroyed and they
can be destroyed only one at a time.

You need to write a program that tells Neo the minimum amount of time he will require to disrupt the connection among machines.

 
Input
The first line is an integer T represents there are T test cases. (0

 
Output
For each test case print the minimum time required to disrupt the connection among Machines.
 
Sample Input
1 5 3 2 1 8 1 0 5 2 4 5 1 3 4 2 4 0
 
Sample Output
10
题解:这题就是一些机器人在不同城市,通过毁坏一定的路,使他们不能相互联系;毁坏路需要时间,求最短时间;
解这个题需要先对时间从大到小排序,从大到小是为了让那些没被标记的陷进去,到最后出现被标记的路时就小了,所需时间就少了;
注意的是vis的记录,还有要让被标记的当老大;
代码:
 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN=;
struct Node{
int s,e,t;
};
Node dt[MAXN];
/*int cmp(const void *a,const void *b){
return (*(Node *)a).t-(*(Node *)b).t) ;//
}*/
int cmp(Node a,Node b){
return a.t>b.t;
}
int pre[MAXN];
int find(int x){
return pre[x]= x==pre[x]?x:find(pre[x]);
}
int visit[MAXN],n,m,flot;
__int64 time;
void initial(){
memset(visit,,sizeof(visit));
memset(pre,-,sizeof(pre));
time=;flot=;
}
void merge(Node a){
int f1,f2;
if(pre[a.s]==-)pre[a.s]=a.s;
if(pre[a.e]==-)pre[a.e]=a.e;
f1=find(a.s);f2=find(a.e);
if(f1==f2)return;
if(visit[f1]&&visit[f2]){
time+=a.t;
flot++;
// printf("%d %d\n",a.s,a.e);
}
else if(f1!=f2){
if(visit[f1])pre[f2]=f1;
else pre[f1]=f2;
}
}
int main(){
int T,temp;
scanf("%d",&T);
while(T--){
initial();
scanf("%d%d",&n,&m);
for(int i=;i<n-;i++){
scanf("%d%d%d",&dt[i].s,&dt[i].e,&dt[i].t);
}
///qsort(dt,n-1,sizeof(dt[0]),cmp);
sort(dt,dt+n-,cmp);
for(int i=;i<m;i++){
scanf("%d",&temp);
visit[temp]=;
}
for(int i=;i<n-;i++){
merge(dt[i]);
if(flot==m-)break;
}
printf("%I64d\n",time);
}
return ;
}

Matrix(类似kruskal)的更多相关文章

  1. BZOJ 1196 [HNOI2006]公路修建问题:二分 + 贪心生成树check(类似kruskal)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1196 题意: n个城市,m对城市之间可以修公路. 公路有两种,一级公路和二级公路,在第i对 ...

  2. (贪心)kruskal思想

    hdu4313 Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  4. BZOJ4883 棋盘上的守卫 基环树、Kruskal

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=4883 题意:给出一个$N \times M$的棋盘,每个格子有权值.你需要每一行选中一 ...

  5. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  6. UVA 1664 Conquer a New Region (Kruskal,贪心)

    题意:在一颗树上要求一个到其他结点容量和最大的点,i,j之前的容量定义为i到j的路径上的最小边容量. 一开始想过由小到大的去分割边,但是很难实现,其实换个顺序就很容易做了,类似kruskal的一个贪心 ...

  7. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

    Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...

  8. Codeforces #270 D. Design Tutorial: Inverse the Problem

    http://codeforces.com/contest/472/problem/D D. Design Tutorial: Inverse the Problem time limit per t ...

  9. Ideas and Tricks

    1.树上拓扑排序计数 结论$\dfrac{n!}{\prod\limits_{i=1}^n size_i}$ 对于节点$i$,其子树随意排序的结果是$size[i]!$ 但$i$需要排在第一位,只有$ ...

随机推荐

  1. android的edittext输入长度

    http://blog.csdn.net/uyu2yiyi/article/details/6329738 http://flysnow.iteye.com/blog/828415/ http://s ...

  2. STM32F103控制两个步进电机按照一定转速比运动

    这个暑假没有回家,在学校准备九月份的电子设计竞赛.今天想给大家分享一下STM32定时器控制两个步进电机按照一定速度比转动的问题. 这次做的05年的电子设计竞赛题目,运动悬挂系统..本实验是控制两个步进 ...

  3. Linux进程间通信——使用命名管道

    在前一篇文章——Linux进程间通信——使用匿名管道中,我们看到了如何使用匿名管道来在进程之间传递数据,同时也看到了这个方式的一个缺陷,就是这些进程都由一个共同的祖先进程启动,这给我们在不相关的的进程 ...

  4. jquery 单击table行事件和radio的选中事件冲突

    原文地址:http://zhidao.baidu.com/link?url=HER7lu4jqejWUhWQO2nq6LZ6tf7vyhPZRADSL-xaBQSF4P4yftD9vg08Ss8HF- ...

  5. poj 1907 Work Reduction_贪心

    题意:公司要你要完成N份任务,但是你是不可能全部完成的,所以需要雇佣别人来做,做到剩下M份时,自己再亲自出马.现在有个机构,有两种付费方式,第一种是每付A元帮你完成1份,第二种是每付B元帮你完成剩下任 ...

  6. 打包静态库.a文件的方法(ar,ranlib,nm命令介绍)

    一 常用脚本 1 打包脚本 脚本如下,下面附上ar 和 ranlib命令参考(命令来自于网络) ALLLIB=*.aFILE=`ls *.a`#原来的库解压重命名 for F in $FILEdo   ...

  7. hdu 5607 graph (矩阵乘法快速幂)

    考虑一个经典的问题: 询问从某个点出发,走 k 步到达其它各点的方案数? 这个问题可以转化为矩阵相乘,所以矩阵快速幂即可解决. 本题思路: 矩阵经典问题:求从i点走k步后到达j点的方案数(mod p) ...

  8. Dollar Dayz(大数母函数,高低位存取)

    Dollar Dayz Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5655   Accepted: 2125 Descr ...

  9. 滚动栏范围位置函数(SetScrollRange、SetScrollPos、GetScrollRange、GetScrollPos)

    滚动栏的范围是一对整数,默认情况下,滚动栏的范围是0~100. SetScrollRange(hwnd,iBar,iMin,iMax,bRedraw)这里的iBar參数要么是SB_VERT,要么是SB ...

  10. _itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0; }

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenVveW91MTMxNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...