POJ2139-有关%.0lf与(int)强制类型转换的区别,用Dijkstra写的单源点最短路径

来源:互联网 发布:绝地求生有数据接口吗 编辑:程序博客网 时间:2024/06/06 10:09

Six Degrees of Cowvin Bacon

SubmitStatusPracticePOJ 2139

Description

The cows have been making movies lately, so they are ready to play a variant of the famous game "Six Degrees of Kevin Bacon".

The game works like this: each cow is considered to be zero degrees of separation (degrees) away from herself. If two distinct cows have been in a movie together, each is considered to be one 'degree' away from the other. If a two cows have never worked together but have both worked with a third cow, they are considered to be two 'degrees' away from each other (counted as: one degree to the cow they've worked with and one more to the other cow). This scales to the general case.

The N (2 <= N <= 300) cows are interested in figuring out which cow has the smallest average degree of separation from all the other cows. excluding herself of course. The cows have made M (1 <= M <= 10000) movies and it is guaranteed that some relationship path exists between every pair of cows.

Input

* Line 1: Two space-separated integers: N and M

* Lines 2..M+1: Each input line contains a set of two or more space-separated integers that describes the cows appearing in a single movie. The first integer is the number of cows participating in the described movie, (e.g., Mi); the subsequent Mi integers tell which cows were.

Output

* Line 1: A single integer that is 100 times the shortest mean degree of separation of any of the cows.

Sample Input

4 23 1 2 32 3 4

Sample Output

100

Hint

[Cow 3 has worked with all the other cows and thus has degrees of separation: 1, 1, and 1 -- a mean of 1.00 .]

代码如下:

#include<stdio.h>#include<string.h>#define INF 1000000#define MAXN 310int n;int s[MAXN],dist[MAXN];int edge[MAXN][MAXN];double dijkstra(int v0){    double ma=0;    int i,j,k;    for(i=0; i<n; i++)    {        dist[i]=edge[v0][i];        s[i]=0;    }    s[v0]=1;    for(i=0; i<n-1; i++)    {        int MIN=INF,u;        for(j=0; j<n; j++)        {            if(!s[j]&&dist[j]<MIN)            {                MIN=dist[j];                u=j;            }        }        s[u]=1;        for(k=0; k<n; k++)        {            if(!s[k]&&edge[u][k]<INF&&dist[u]+edge[u][k]<dist[k])                dist[k]=dist[u]+edge[u][k];        }    }    for(i=0; i<n; i++)    {        ma+=dist[i];    }    return ma;}int main(){    int m,i,j,k,t;    int T[10001];    double Dijkstra[MAXN];    double MINN=10000000;    scanf("%d%d",&n,&m);    for(i=0; i<m; i++)    {        scanf("%d",&t);        for(j=0; j<t; j++)        {            scanf("%d",&T[j]);        }        for(j=0; j<t; j++)        {            for(k=j+1; k<t; k++)            {                edge[T[j]-1][T[k]-1]=1;                edge[T[k]-1][T[j]-1]=1;            }        }    }    for(i=0; i<n; i++)    {        for(j=0; j<n; j++)        {            if(i==j)            {                edge[i][j]=0;                edge[j][i]=0;            }            else if(edge[i][j]==0)            {                edge[i][j]=INF;                edge[j][i]=INF;            }        }    }    for(j=0; j<n; j++)    {        Dijkstra[j]=dijkstra(j);        if(Dijkstra[j]<MINN) MINN=Dijkstra[j];    }    printf("%d\n",(int)(MINN/(n-1)*100));    return 0;}
刚开始的输出是这样写的:

printf("%.0lf\n",(MINN/(n-1)*100);是WA

因为用.0lf的话,会自动四舍五入,用(int)强制类型转换的话,不会四舍五入,会直接输出整数部分

0 0
原创粉丝点击