Connect the Cities

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12903    Accepted Submission(s):
3549

Problem Description
In 2100, since the sea level rise, most of the cities
disappear. Though some survived cities are still connected with others, but most
of them become disconnected. The government wants to build some roads to connect
all of these cities again, but they don’t want to take too much money.  
 
Input
The first line contains the number of test
cases.
Each test case starts with three integers: n, m and k. n (3 <= n
<=500) stands for the number of survived cities, m (0 <= m <= 25000)
stands for the number of roads you can choose to connect the cities and k (0
<= k <= 100) stands for the number of still connected cities.
To make
it easy, the cities are signed from 1 to n.
Then follow m lines, each
contains three integers p, q and c (0 <= c <= 1000), means it takes c to
connect p and q.
Then follow k lines, each line starts with an integer t (2
<= t <= n) stands for the number of this connected cities. Then t integers
follow stands for the id of these cities.
 
Output
For each case, output the least money you need to take,
if it’s impossible, just output -1.
 
Sample Input
1
6 4 3
1 4 2
2 6 1
2 3 5
3 4 33
2 1 2
2 1 3
3 4 5 6
 
Sample Output
1
 
prime算法
#include<stdio.h>
#include<string.h>
#define MAX 550
#define INF 0x3f3f3f
int city;
int visit[MAX],map[MAX][MAX],low[MAX];
void prime()
{
int j,i,min,mincost=0,next;
memset(visit,0,sizeof(visit));
for(i=1;i<=city;i++)
{
low[i]=map[1][i];
}
visit[1]=1;
for(i=1;i<city;i++)
{
min=INF;
for(j=1;j<=city;j++)
{
if(!visit[j]&&min>low[j])
{
next=j;
min=low[j];
}
}
if(min==INF)
{
printf("-1\n");
return ;
}
mincost+=min;
visit[next]=1;
for(j=1;j<=city;j++)
{
if(!visit[j]&&low[j]>map[next][j])
low[j]=map[next][j];
}
}
printf("%d\n",mincost);
}
int main()
{
int n,m,j,i,k,q,p,c,t,x,y,l;
int a[MAX];
scanf("%d",&n);
while(n--)
{
scanf("%d%d%d",&city,&m,&k);
for(i=1;i<=city;i++)
{
for(j=1;j<=city;j++)
{
if(i==j)
map[i][j]=0;
else
map[i][j]=INF;
}
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&p,&q,&c);
if(map[p][q]>c)
map[p][q]=map[q][p]=c;
}
while(k--)
{
scanf("%d",&t);
for(l=1;l<=t;l++)
scanf("%d",&a[l]);
for(i=1;i<t;i++)
{
for(j=i+1;j<=t;j++)
{
map[a[i]][a[j]]=map[a[j]][a[i]]=0;
}
}
}
prime();
}
return 0;
}

  

hdoj 3371 Connect the Cities的更多相关文章

  1. hdu 3371 Connect the Cities

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...

  2. HDU 3371 Connect the Cities(prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...

  3. hdu 3371 Connect the Cities(最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...

  4. hdu oj 3371 Connect the Cities (最小生成树)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. hdu 3371 Connect the Cities (最小生成树Prim)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...

  6. Hdu 3371 Connect the Cities(最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...

  7. HDU 3371 Connect the Cities(并查集+Kruskal)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...

  8. POJ:3371 Connect the Cities(最小生成树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3371 AC代码: /** /*@author Victor /* C++ */ #include <bit ...

  9. HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)

    解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...

随机推荐

  1. Spring mvc 模式小结

    http://www.taobaotesting.com/blogs/2375 1.spring mvc简介 Spring MVC框架是一个MVC框架,通过实现Model-View-Controlle ...

  2. cocos2dx Sprite setBlendFunc 使用颜色混合:加算,减算

    说明: 图片的混色是游戏的特效的特破口,应用的好,可以大量减少图片的使用量,有专门的介绍资料... setBlendFunc  相关参数,有多种组合方式,需要经验的积累 http://blog.csd ...

  3. eclipse实现JavaWeb应用增量打包

    很多情况下,项目是不允许全量发布的,所以你得把有做修改的文件一个个挑出来,如果有成千上百的文件,你是不是要头大了? 以下方法应该可以让你得到解救!前提是你是用装有svn plugin的eclipse上 ...

  4. php 23种设计模式的趣味解释

    http://wenku.baidu.com/link?url=GwvuvSOdJneZQc-DSKoGmPcxTtzn3cdtIp3fRaCNbkg1zJDZZZTx2NwEK5IsqU996fG3 ...

  5. strcpy函数的C/C++实现

    2013-07-05 14:07:49 本函数给出了几种strcpy与strncpy的实现,有ugly implementation,也有good implementation.并参考标准库中的imp ...

  6. WinAPI——SetWindowsHookEx设置钩子说明

    提示: 如果要设置系统级钩子, 钩子函数必须在 DLL 中. SetWindowsHookEx(   idHook: Integer;  {钩子类型}   lpfn: TFNHookProc; {函数 ...

  7. Android开发之技术文章索引

    Activity: 1.PreferenceActivity Fragment: 1.fragment中套用PagerSlidingTabStrip,切换底部时viewpager消失的解决 Widge ...

  8. Redis文档

    http://manual.csser.com/redis/connection/auth.html

  9. Java [leetcode 27]Remove Element

    题目描述: Given an array and a value, remove all instances of that value in place and return the new len ...

  10. postgresql 行转列,拼接字符串

    create table k_user ( op_id ) not null, op_name ) not null, password ) not null, real_name ) not nul ...