poj1258

 Agri-Net

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 25093 Accepted: 9868
Description
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.

Input

The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

Output

For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

Sample Input

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

Sample Output

28

很显然求这个图的最小生成树

然后我就打算写prim了,写完了之后觉得没错了,但是交了好几遍都wa了,

真心不知道错误在哪里,然后去看discuss,才注意到这句话

Physically, they are limited in length to 80 characters, so some lines continue onto others.

当时看题时候没看明白,看别人说的,没行还有多余的数据


我就恶心了,我以前写pascal还有readln可用,现在C语言有不是很熟,用什么代替呢,想了半天没想出来

还有人说数据范围不止这些,不过后来验证后,数据范围就是100

我就去找题解了

好多题解我发现都没注意这一点,然后我就纳闷了,有看discuss,又有人说,不用注意这句话,我就纳闷了,我找了个题解交了之后居然过了

我就在想,是不是我的prim出错了呢

结果一不小心瞥见,ans居然和vis一块定义的,定义成short了,

呃……直接无语了

#include<stdio.h>
#include
<string.h>
#include
<math.h>
#define MAX 505
int map[MAX][MAX],cost[MAX];
short vis[MAX];
int ans;
int n;
void prim()
{
    
int i,j,mini,min;
    memset(vis,
0,sizeof(vis));
    vis[
1]=1;
    ans
=0;
    
for (i=1; i<=n; i++)
        cost[i]
=0x7fffffff;
    
for (i=2; i<=n; i++)
            cost[i]
=map[1][i];
    
for (i=1; i<=n-1; i++)
    
{
        min
=0x7fffffff;
        
for (j=1; j<=n; j++)
            
if ((vis[j]==0)&&(cost[j]<min))
            
{
                min
=cost[j];
                mini
=j;
            }

        vis[mini]
=1;
        ans
=ans+min;
        
for (j=1; j<=n ; j++ )
            
if ((vis[j]==0)&&(map[mini][j]>0)&&(map[mini][j]<cost[j]))
                cost[j]
=map[mini][j];
    }

}

void init()
{
    
int i,j;
    memset(map,
0,sizeof(map));
    
for (i=1; i<=n ; i++ )
    
{
        
for (j=1; j<=n ; j++ )
        
{
            scanf(
"%d",&map[i][j]);
        }

    }

}

int main()
{
    ;
    
while (scanf("%d",&n)!=EOF)
    
{
        init();
        prim();
        printf(
"%d\n",ans);
    }

    
return 0;
}


 

posted on 2012-02-14 12:57 jh818012 阅读(183) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年7月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

常用链接

留言簿

文章档案(85)

搜索

最新评论