HDU 1102 最小生成树
来源:互联网 发布:vb datagrid 列不显示 编辑:程序博客网 时间:2024/06/06 07:09
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17560 Accepted Submission(s): 6683
Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
Sample Input
30 990 692990 0 179692 179 011 2
Sample Output
179
已经建好的 设为0
#include<iostream>
#include<cstdio>
#define INF 99999999
using namespace std;
int map[105][105]={0};
bool used[105]={0};
int mincost[105]={0};
int n=0;
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
used[i]=0;
mincost[i]=INF;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
}
int q=0;
scanf("%d",&q);
while(q--)
{
int x=0,y=0;
scanf("%d%d",&x,&y);
map[x][y]=map[y][x]=0;
}
int res=0;
mincost[1]=0;
while(1)
{
int v=-1;
for(int u=1;u<=n;u++)
if(!used[u]&&(v==-1||mincost[u]<mincost[v])) v=u;
if(v==-1)
break;
used[v]=1;
if(mincost[v]!=INF)
res+=mincost[v];
for(int u=1;u<=n;u++)
{
if(mincost[u]>map[v][u])
{
mincost[u]=map[v][u];
}
}
}
printf("%d\n",res);
}
return 0;
}
#include<cstdio>
#define INF 99999999
using namespace std;
int map[105][105]={0};
bool used[105]={0};
int mincost[105]={0};
int n=0;
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
used[i]=0;
mincost[i]=INF;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
}
int q=0;
scanf("%d",&q);
while(q--)
{
int x=0,y=0;
scanf("%d%d",&x,&y);
map[x][y]=map[y][x]=0;
}
int res=0;
mincost[1]=0;
while(1)
{
int v=-1;
for(int u=1;u<=n;u++)
if(!used[u]&&(v==-1||mincost[u]<mincost[v])) v=u;
if(v==-1)
break;
used[v]=1;
if(mincost[v]!=INF)
res+=mincost[v];
for(int u=1;u<=n;u++)
{
if(mincost[u]>map[v][u])
{
mincost[u]=map[v][u];
}
}
}
printf("%d\n",res);
}
return 0;
}
0 0
- HDU 1102 最小生成树
- hdu 1102 最小生成树
- HDU 1102(最小生成树)
- hdu 1102 (最小生成树)
- HDU 1102 (最小生成树)
- HDU 1102 最小生成树
- HDU 1102 最小生成树
- hdu 1102 (最小生成树)
- HDU 1102 (最小生成树)
- HDU 1102(最小生成树)
- HDU-1102(prim求最小生成树)
- hdu 1102 (最小生成树)
- hdu 1102(最小生成树)
- hdu 1102 Constructing Roads 最小生成树
- Constructing Roads - HDU 1102 最小生成树
- hdu 1102 Constructing Roads 最小生成树
- Constructing Roads(HDU 1102 最小生成树)
- hdu 1102 Constructing Roads 最小生成树
- html5 canvas 详细使用教程
- Java中的break,continue,return
- Knockout应用开发指南(完整版) 目录索引
- 黑马程序员----Java集合框架学习笔记2 Map-工具类-泛型
- java类型转换
- HDU 1102 最小生成树
- Cocos2d-x中,Speed类的使用
- [深入Python]__new__和__init__
- PIC之中断
- Mysql 会导致锁表的语法
- 解决Python "no OPENSSL_Applink"错误
- 笔记本惠普CQ14-a003TX加装内存和SSD固态硬盘
- javascript函数
- Python 技巧:@classmethod 修饰符