http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=198#problem/E
水题一道,题意就是让求一遍最小生成树与最大生成树,但我因为Each case contains a blank line and an integer n (1 ≤ n ≤ 100) denoting the number of houses.这句话错了三遍,有两种解决方案,一不用管空格,二gets(str);
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define N 1000001
int map[101][101],map1[101][101];
int v[101],v1[101];
int dis[101],dis1[101];
int n;
int Primmin()
{
int min,k,sum=0;
memset(v,0,sizeof(v));
v[0]=1;
for(int i=0;i<=n;i++)
dis[i]=map[0][i];
for(int i=0;i<n;i++)
{
min=N;
for(int j=0;j<=n;j++)
{
if(v[j]==0&&min>dis[j])
{
min=dis[j];
k=j;
}
}
v[k]=1;
sum=sum+min;
for(int j=0;j<=n;j++)
{
if(v[j]==0&&dis[j]>map[k][j])
{
dis[j]=map[k][j];
}
}
}
return sum;
}
int Primmax()
{
int min,k,sum=0;
memset(v1,0,sizeof(v1));
v1[0]=1;
for(int i=0;i<=n;i++)
dis1[i]=map1[0][i];
for(int i=0;i<n;i++)
{
min=0;
for(int j=0;j<=n;j++)
{
if(v1[j]==0&&min<dis1[j])
{
min=dis1[j];
k=j;
}
}
sum=sum+min;
v1[k]=1;
for(int j=0;j<=n;j++)
{
if(v1[j]==0&&dis1[j]<map1[k][j])
{
dis1[j]=map1[k][j];
}
}
}
return sum;
}
int main()
{
int T,K=0;
char ch[101];
scanf("%d",&T);
while(T--)
{
K++;
gets(ch);
scanf("%d",&n);
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
map[i][j]=N;
map[j][i]=N;
}
map[i][i]=0;
}
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
map1[i][j]=0;
map1[j][i]=0;
}
}
int xx,yy,zz;
while(scanf("%d%d%d",&xx,&yy,&zz)!=EOF)
{
if(xx==0&&yy==0&&zz==0) break;
if(map[xx][yy]>zz)
{
map[xx][yy]=zz;
map[yy][xx]=zz;
}
if(map1[xx][yy]<zz)
{
map1[xx][yy]=zz;
map1[yy][xx]=zz;
}
}
int sum1=Primmin();
int sum2=Primmax();
long long int tt=sum1+sum2;
if(tt%2==0)
{
tt=tt/2;
printf("Case %d: %lld\n",K,tt);
}
else printf("Case %d: %lld/2\n",K,tt);
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.