Matrix(类似kruskal)

本文探讨了一种算法问题,即通过销毁特定路径来阻止不同城市的机器人间的联络,旨在找到所需的最短时间。该问题可通过一种特殊的并查集算法解决,通过对路径按时间逆序排序并采用特定的合并策略来实现。

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的记录,还有要让被标记的当老大;
代码:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 const int MAXN=100010;
 6 struct Node{
 7     int s,e,t;
 8 };
 9 Node dt[MAXN];
10 /*int cmp(const void *a,const void *b){
11     return (*(Node *)a).t-(*(Node *)b).t)  ;//
12 }*/
13 int cmp(Node a,Node b){
14     return a.t>b.t;
15 }
16 int pre[MAXN];
17 int find(int x){
18     return pre[x]= x==pre[x]?x:find(pre[x]);
19 }
20 int visit[MAXN],n,m,flot;
21 __int64 time;
22 void initial(){
23     memset(visit,0,sizeof(visit));
24     memset(pre,-1,sizeof(pre));
25     time=0;flot=0;
26 }
27 void merge(Node a){
28     int f1,f2;
29     if(pre[a.s]==-1)pre[a.s]=a.s;
30     if(pre[a.e]==-1)pre[a.e]=a.e;
31     f1=find(a.s);f2=find(a.e);
32     if(f1==f2)return;
33     if(visit[f1]&&visit[f2]){
34         time+=a.t;
35         flot++;
36     //    printf("%d %d\n",a.s,a.e);
37     }
38     else if(f1!=f2){
39         if(visit[f1])pre[f2]=f1;
40         else pre[f1]=f2;
41     }
42 }
43 int main(){
44     int T,temp;
45     scanf("%d",&T);
46         while(T--){
47             initial();
48             scanf("%d%d",&n,&m);
49             for(int i=0;i<n-1;i++){
50                 scanf("%d%d%d",&dt[i].s,&dt[i].e,&dt[i].t);
51             }
52             ///qsort(dt,n-1,sizeof(dt[0]),cmp);
53             sort(dt,dt+n-1,cmp);
54             for(int i=0;i<m;i++){
55                 scanf("%d",&temp);
56                 visit[temp]=1;
57             }
58             for(int i=0;i<n-1;i++){
59                 merge(dt[i]);
60                 if(flot==m-1)break;
61             }
62             printf("%I64d\n",time);
63         }
64     return 0;
65 }

 

转载于:https://www.cnblogs.com/handsomecui/p/4727917.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值