POJ2139-有关%.0lf与(int)强制类型转换的区别,用Dijkstra写的单源点最短路径
来源:互联网 发布:绝地求生有数据接口吗 编辑:程序博客网 时间:2024/06/06 10:09
Six Degrees of Cowvin Bacon
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.
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.
* 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
- POJ2139-有关%.0lf与(int)强制类型转换的区别,用Dijkstra写的单源点最短路径
- 单源点最短路径Dijkstra算法的JAVA实现
- 单源点最短路径Dijkstra算法的JAVA实现
- 单源点最短路径Dijkstra的java实现
- 单源点最短路径的Dijkstra算法
- 图的最短路径(三)-单源点最短路径(Dijkstra算法)
- 单源点最短路径----Dijkstra算法
- 单源点最短路径(dijkstra算法)
- Dijkstra求解单源点最短路径
- Dijkstra 邻接矩阵 单源点最短路径
- 单源点最短路径算法的设计与实现
- 单源点的最短路径
- PKU1062(昂贵的聘礼)单源点最短路径-Dijkstra算法
- 有向图中单个源点到终点的最短路径--Dijkstra算法与实现
- 单源点最短路径算法(dijkstra)
- 单源点最短路径问题(Dijkstra算法)
- 单源点最短路径Dijkstra和Bellmanford
- 【数据结构】算法7.15 Dijkstra算法 单源点最短路径
- response重定向和request转发的去呗
- 文件下载
- 15周《C++语言基础》程序阅读——二进制文件及文件的随机读写(1)
- 【hdoj 1242】Rescue
- hibernate关联映射(二)
- POJ2139-有关%.0lf与(int)强制类型转换的区别,用Dijkstra写的单源点最短路径
- 基于Gstreamer的TI DSP视频处理框架
- 方法重写和方法重载的区别
- PHP中WebService应用_SOAP
- c++11 对容器进行排序
- 数据结构算法代码实现——栈和队列(二)
- 剑指offer--面试题16:翻转链表--Java实现
- [java]类加载机制
- hdu 1159 整理下水题 递推动规