The plan of city rebuild

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 616    Accepted Submission(s): 215

Problem Description
News comes!~City W will be rebuilt with the expectation to become a center city. There are some villages and roads in the city now, however. In order to make the city better, some new villages should be built and some old ones should be destroyed. Then the
officers have to make a new plan, now you , as the designer, have the task to judge if the plan is practical, which means there are roads(direct or indirect) between every two villages(of course the village has not be destroyed), if the plan is available,
please output the minimum cost, or output"what a pity!".
 
Input
Input contains an integer T in the first line, which means there are T cases, and then T lines follow.

Each case contains three parts. The first part contains two integers l(0<l<100), e1, representing the original number of villages and roads between villages(the range of village is from 0 to l-1), then follows e1 lines, each line contains three integers a,
b, c (0<=a, b<l, 0<=c<=1000), a, b indicating the village numbers and c indicating the road cost of village a and village b . The second part first contains an integer n(0<n<100), e2, representing the number of new villages and roads(the range of village is
from l to l+n-1), then follows e2 lines, each line contains three integers x, y, z (0<=x, y<l+n, 0<=z<=1000), x, y indicating the village numbers and z indicating the road cost of village x and village y. The third part contains an integer m(0<m<l+n), representing
the number of deserted villages, next line comes m integers, p1,p2,…,pm,(0<=p1,p2,…,pm<l+n) indicating the village number. 

Pay attention: if one village is deserted, the roads connected are deserted, too.
 
Output
For each test case, If all villages can connect with each other(direct or indirect), output the minimum cost, or output "what a pity!".
 
Sample Input
2
4 5
0 1 10
0 2 20
2 3 40
1 3 10
1 2 70
1 1
4 1 60
2
2 3
3 3
0 1 20
2 1 40
2 0 70
2 3
0 3 10
1 4 90
2 4 100
0
 
Sample Output
70
160
 
Author
wangjing
 


题目大意:先给你n1个点。m1条边。又给你n2个点。m2条边。然后告诉你有些点不须要。然后求剩下点的最

小生成树。

反正光看题目就花了半小时。真不明确了,老路,新路,给边还分两次给。。


用了prim和kruskal两种算法来实现。中间还debug了蛮久。




Prim算法:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF =1e8;
int mp[205][205];
int des[205];
int visi[205];
int low[205]; //更新最小值
int n; int prim()
{
int i,j; memset(visi,0,sizeof(visi));
for(i=0;i<=200;i++)
low[i]=INF;
int ans=0,pos=-1;
for(i=0;i<n;i++)
{
if(!des[i])
{
pos=i;
break;
}
} if(pos==-1) return 0; for(i=0;i<=200;i++)
low[i]=mp[pos][i]; visi[pos]=1;
int mi;
for(i=0;i<n;i++)
{
mi=INF;
int flag=0;
for(j=0;j<n;j++)
{
if(des[j]) continue;
if(!visi[j])
{
flag=1;
if(low[j]<mi)
{
mi=low[j];
pos=j;
}
}
} if(mi==INF&&flag)
return -1;
if(!flag) return ans;
ans+=mi;
visi[pos]=1;
for(j=0;j<n;j++)
if(!visi[j])
low[j]=min(low[j],mp[pos][j]);
}
return ans;
} int main()
{
int tes,i,j,res;
scanf("%d",&tes); while(tes--)
{
int n1,m,u,v,val;
scanf("%d%d",&n1,&m); for(i=0;i<=200;i++)
for(j=0;j<=200;j++)
mp[i][j]=INF;
memset(des,0,sizeof(des));
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&val);
if(mp[u][v]>val) //这个地方。边可能会多次给出,坑。。
{
mp[u][v]=val;
mp[v][u]=val;
}
}
int n2;
scanf("%d%d",&n2,&m);
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&val);
if(mp[u][v]>val)
{
mp[u][v]=val;
mp[v][u]=val;
}
}
n=n1+n2;
int t,x;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&x);
des[x]=1;
} res=prim();
if(res==-1) puts("what a pity!");
else printf("%d\n",res);
}
return 0;
}

Kruskal算法:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF =1e6;
const int maxn=205; int n,m,t; //总点数,总边数,desert的边数 int des[maxn]; //为1代表城市荒废
int fa[maxn]; struct Edge
{
int from;
int to;
int val;
}edge[maxn*maxn]; int cmp(Edge p1,Edge p2)
{
//if(des[p1.from]||des[p1.to]) return 0;
//if(des[p2.from]||des[p2.to]) return 1;
return p1.val<p2.val;
} void init()
{
for(int i=0;i<=200;i++)
fa[i]=i;
} int find1(int x)
{
if(fa[x]!=x) fa[x]=find1(fa[x]);
return fa[x];
} void merge1(int p1,int p2)
{
p1=find1(p1);
p2=find1(p2);
fa[p1]=p2;
} int Kruskal()
{
sort(edge,edge+m,cmp);
int ans=0,ste=0; for(int i=0;i<m&&ste<n-t-1;i++)
{
int u=edge[i].from,v=edge[i].to,val=edge[i].val;
if(des[u]||des[v]) continue;
if(find1(u)!=find1(v))
{
ste++;
ans+=val;
merge1(u,v);
}
} if(ste==n-t-1) return ans;
return -1;
} int main()
{
int tes,i,j,res;
scanf("%d",&tes); while(tes--)
{
int n1,m1,u,v,val,n2,m2;
scanf("%d%d",&n1,&m1); memset(des,0,sizeof(des)); for(i=0;i<m1;i++)
{
scanf("%d%d%d",&u,&v,&val);
edge[i].from=u;
edge[i].to=v;
edge[i].val=val;
} scanf("%d%d",&n2,&m2);
n=n1+n2;
m=m1+m2; for(i=m1;i<m;i++)
{
scanf("%d%d%d",&u,&v,&val);
edge[i].from=u;
edge[i].to=v;
edge[i].val=val;
} int x;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&x);
des[x]=1;
} init();
res=Kruskal(); //cout<<n<<" :n"<<endl;
if(res==-1) puts("what a pity!");
else printf("%d\n",res);
}
return 0;
}

HDU 3080 The plan of city rebuild(prim和kruskal)的更多相关文章

  1. HDU 3080 The plan of city rebuild(除点最小生成树)

    题意  一个城市原来有l个村庄 e1条道路  又添加了n个村庄 e2条道路  后来后销毁了m个村庄  与m相连的道路也销毁了  求使全部未销毁村庄相互连通最小花费  不能连通输出what a pity ...

  2. hdu 1233 还是畅通工程 最小生成树(prim算法 + kruskal算法)

    还是畅通工程                                                                            Time Limit: 4000/2 ...

  3. Prim和Kruskal最小生成树

    标题: Prim和Kruskal最小生成树时 限: 2000 ms内存限制: 15000 K总时限: 3000 ms描述: 给出一个矩阵,要求以矩阵方式单步输出生成过程.要求先输出Prim生成过程,再 ...

  4. 【图论】信手拈来的Prim,Kruskal和Dijkstra

    关于三个简单的图论算法 prim,dijkstra和kruskal三个图论的算法,初学者容易将他们搞混,所以放在一起了. prim和kruskal是最小生成树(MST)的算法,dijkstra是单源最 ...

  5. 图的最小生成树的理解和实现:Prim和Kruskal算法

    最小生成树 一个连通图的生成树是一个极小的连通子图,它含有图中所有的顶点,但只有足以构成一棵树的n-1条边.我们将构造连通网的最小代价生成树称为最小生成树(Minimum Cost Spanning ...

  6. 最小生成树(prim和kruskal)

    最小生成树(prim和kruskal) 最小生成树的最优子结构性质 设一个最小生成树是T.如果选出一个T中的一条边,分裂成的两个树T1,T2依然是它们的点集组成的最小生成树.这可以用反证法来证.反着来 ...

  7. 最小生成树,Prim和Kruskal的原理与实现

    文章首先于微信公众号:小K算法,关注第一时间获取更新信息 1 新农村建设 大清都亡了,我们村还没有通网.为了响应国家的新农村建设的号召,村里也开始了网络工程的建设. 穷乡僻壤,人烟稀少,如何布局网线, ...

  8. 最小生成树MST算法(Prim、Kruskal)

    最小生成树MST(Minimum Spanning Tree) (1)概念 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边,所谓一个 ...

  9. 2015 Multi-University Training Contest 1 hdu 5290 Bombing plan

    Bombing plan Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

随机推荐

  1. 查询SYS_ORG_TB树的层级

    WITH N(SYS_ORG_ID,SYS_ORG_NAME,LEVEL) AS( AS LEVEL FROM SYS_ORG_TB WHERE SYS_ORG_UPID IS NULL UNION ...

  2. 深入理解JavaScript的设计模式

    使用适当的设计模式可以帮助你编写更好.更易于理解的代码.这样的代码也更容易维护.但是,重要的是不要过度使用它们.在使用设计模式之前,你应该仔细考虑你的问题是否符合设计模式. 当你开始一个新的项目时,你 ...

  3. Linux文本检索命令grep笔记

    grep是在linux系统中基于行文本非常实用检索工具,通过该命令可以将匹配到的结果信息输出到终端控制台. 语法格式:grep [-ivnc] '需要匹配的内容' 文件名 常用参数说明: -i 检索的 ...

  4. python-----定制群发微信消息

    如何使用表格中的信息群发微信消息? 如何读取csv? →   使用内置模块csv 如何按对应信息发送到微信?→  使用第三方库wxpy 以下代码素材自取:链接:https://pan.baidu.co ...

  5. python多线程--优先级队列(Queue)

    Python的Queue模块中提供了同步的.线程安全的队列类,包括FIFO(先入先出)队列Queue,LIFO(后入先出)队列LifoQueue,和优先级队列PriorityQueue.这些队列都实现 ...

  6. 成为七牛云 Contributor -如何贡献 logkit 代码

    logkit 是 Pandora 开源的一个通用的日志收集工具,可以将不同数据源的数据方便的发送到 Pandora 进行数据分析.除了基本的数据发送功能,logkit 还有容错.并发.监控.删除等功能 ...

  7. POJ1450:Gridland 【杂题】

    题目大意:先给出了TSP的背景,然后给出一个n*m的单位格点的图,图中除边缘上的点与八个方向的点有边连接,距离为欧拉距离,求从左上角出发的TSP 思路:从水题列表中看到的题,但看一开始给出的backg ...

  8. jmesa应用

    一直以来,都没发现什么好的分页组件,最初时用过displaytag,仔细研究了一下,发现它并没有别人说的那么强大,至少离自己的期望还很远,因此尝试寻找其它新的分页组件,但很久以来都没发现自己满意的.无 ...

  9. [Vijos] SuperBrother打鼹鼠

    背景 SuperBrother在机房里闲着没事干(再对比一下他的NOIP,真是讽刺啊......),于是便无聊地开始玩“打鼹鼠”...... 描述 在这个“打鼹鼠”的游戏中,鼹鼠会不时地从洞中钻出来, ...

  10. 【BZOJ1225】求正整数(数论)

    题意:对于任意输入的正整数n,请编程求出具有n个不同因子的最小正整数m. n<=50000 思路:记得以前好像看的是maigo的题解 n即为将m分解为质数幂次的乘积后的次数+1之积 经检验只需要 ...