您的位置:首页 > 其它

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

2014-09-01 00:13 344 查看


Connect the Cities

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

Total Submission(s): 10336 Accepted Submission(s): 2941



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


给出n个点,m条边,以及k组已经修好的路,问联通所有城市要需要至少多少的修路费。

g++过不了,c++就可以过而且还是593ms 怎么可以这样呀。。。

目前用的并查集

#include <iostream>
#include <stdio.h>
using namespace std;
int father[505];
int size[505];
struct node
{
int x;
int y;
int w;
}a[25005];
int Find(int x)
{
while (x!=father[x])
{
x=father[x];
}
return x;
}
int cmp(const void *a1,const void *b1)
{
node *a=(node*)a1;
node *b=(node*)b1;
return a->w-b->w;
}
int main()
{
int T,t,n,m,k,ans;
scanf("%d",&T);
while (T--)
{
scanf("%d%d%d",&n,&m,&k);
ans=0;
for (int i=1; i<=n; i++)
{
father[i]=i;
size[i]=i;
}
for (int i=0; i<m; i++)
{
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
}
for (int i=0; i<k; i++)
{
int x,y;
scanf("%d%d",&t,&x);
for (int j=1; j<t; j++)
{
scanf("%d",&y);
int fx=Find(x);
int fy=Find(y);
if(fx!=fy)
{
if(size[fx]>size[fy])
{
size[fx]+=size[fy];
father[fy]=fx;
}
else
{
size[fy]+=size[fx];
father[fx]=fy;
}
}

}
}
qsort(a, m, sizeof(a[0]), cmp);
for (int i=0; i<m; i++)
{
int fx=Find(a[i].x);
int fy=Find(a[i].y);
if(fx!=fy)
{
if(size[fx]>size[fy])
{
size[fx]+=size[fy];
father[fy]=fx;
}
else
{
size[fy]+=size[fx];
father[fx]=fy;
}
ans+=a[i].w;
}

}
int count=0;
for (int i=1; i<=n; i++)
{
if(father[i]==i)
{
count++;
}
}
if(count==1)
{
printf("%d\n",ans);
}
else
{
printf("-1\n");
}
}
return 0;
}


Prim

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int map[505][505];
int num[105];
const int Max=10000000;
int n;
void Prim(int v0)
{
int sum=0;
int lowcost[505];
for (int i=0; i<n; i++)
{
lowcost[i]=map[v0][i];
}
lowcost[v0]=-1;
for (int i=1; i<n; i++)
{
int min=Max;
int k=-1;
for (int j=0; j<n; j++)
{
if( lowcost[j]!=-1 && min>lowcost[j])
{
k=j;
min=lowcost[j];
}
}
if(k==-1)
{
printf("-1\n");
return;
}
sum+=lowcost[k];
lowcost[k]=-1;
for (int j=0; j<n; j++)
{
if(lowcost[j]!=-1 && map[k][j]<lowcost[j])
{
lowcost[j]=map[k][j];
}
}
}
printf("%d\n",sum);
}
int main()
{
int t,m,k;
scanf("%d",&t);
while (t--)
{
scanf("%d%d%d",&n,&m,&k);
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
if(i==j)
{
map[i][j]=0;
}
else
{
map[i][j]=Max;
}
}
}
for (int i=0; i<m; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(map[a-1][b-1]>c)
{
map[a-1][b-1]=map[b-1][a-1]=c;
}
}
for (int i=0; i<k; i++)
{
int p;
scanf("%d",&p);
for (int j=0; j<p; j++)
{
scanf("%d",&num[j]);
}
for (int j=0; j<p; j++)
{
for (int x=0; x<p; x++)
{
map[num[j]-1][num[x]-1]=0;   //置为0
}
}
}
Prim(0);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: