poj2139 Six Degrees of Cowvin Bacon(Flyod)

Six Degrees of Cowvin Bacon
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3155 Accepted: 1464

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 2
3 1 2 3
2 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 .] 

Source

USACO 2003 March Orange
/*
注意不要把0给初始化掉。。无语
加油!!!
Time:2014-12-6 19:06
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=1<<20;
const int MAX=300+10;
int n,m;int data[MAX];
int map[MAX][MAX];
void Init(){
	memset(data,0,sizeof(data));
	for(int i=1;i<=n;i++){//注意,不要把0初始化掉。。。。。 
		for(int j=i;j<=n;j++){
			if(i==j)
				map[i][j]=0;
			else
				map[i][j]=map[j][i]=INF;
		}
	}
}
void Floyd(int n){
	for(int k=1;k<=n;k++){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(map[i][j]>map[i][k]+map[k][j])
				map[i][j]=map[j][i]=map[i][k]+map[k][j];
			}
		}
	}
}
int main(){
	int num;
	while(scanf("%d%d",&n,&m)!=EOF){
		Init();
		while(m--){
			scanf("%d",&num);
			for(int i=0;i<num;i++){
				scanf("%d",&data[i]);
				for(int j=0;j<i;j++){
					map[data[j]][data[i]]=map[data[i]][data[j]]=100;
				}
			}
		}
		Floyd(n);
		int minV=INF;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				map[i][0]+=map[i][j];
			}
			if(minV>map[i][0])
				minV=map[i][0];
		}
		printf("%d\n",minV/(n-1));
	}
return 0;
}

### POJ 2139 题目解析 虽然当前引用的内容并未涉及 POJ 2139 的具体描述,但从已有的参考资料来看,我们可以推测这道题可能属于图论或者组合优化类问题。以下是基于常见竞赛编程思路对该题目的解答框架。 #### 假设题目背景 假设 POJ 2139 是关于某种路径规划或资源分配的问题,通常会涉及到最短路、最小生成树或其他经典算法的应用场景。如果它类似于 POJ 2784,则可能是通过构建网络来实现最优连接的目标。 --- #### 解决方案概述 为了找到解决方法,我们先定义几个核心概念: 1. **输入数据结构**: 输入一般包括若干节点及其属性(如坐标)、边权值计算方式以及额外的操作选项(例如购买特定子网的成本)。对于本题而言,需明确如何表示这些要素。 2. **目标函数**: 明确需要最小化的是什么——比如总成本或者是某些特殊条件下的加权距离之和。 3. **主要技术手段**: - 如果存在多个独立集合间的联通需求,可考虑采用 Kruscal 算法结合并查集处理最小生成树问题[^1]; - 若允许部分预置操作降低整体开销,则可通过枚举策略评估不同配置的影响[^2]。 下面给出一段伪代码用于说明逻辑流程: ```python def solve_poj_2139(n, m, edges, packages): parent = list(range(n)) def find(u): while u != parent[u]: parent[u] = parent[parent[u]] u = parent[u] return u def union(u, v): pu, pv = find(u), find(v) if pu == pv: return False parent[pv] = pu return True # Step 1: Process package options and select optimal ones. selected_packages = [] total_cost = 0 for pkg in sorted(packages, key=lambda p:p['cost']): connected_cities = set(find(city) for city in pkg['cities']) if len(connected_cities) > 1: for city in pkg['cities']: parent[find(city)] = min(parent[find(city)], key=find)[pkg['cities'][0]] total_cost += pkg['cost'] selected_packages.append(pkg) # Step 2: Construct MST using remaining edges after applying chosen packages. edges.sort(key=lambda e:e['weight']) # Sort edges based on their weights. for edge in edges: if union(edge['u'], edge['v']): total_cost += edge['weight'] return total_cost, selected_packages ``` 此段程序展示了如何综合运用贪心思想挑选合适套餐与标准MST构造技巧完成任务。 --- #### 关键点分析 - 数据规模较大时需要注意效率问题,在选取合适的工具方面要有所取舍。例如,并查集能够快速判断两顶点是否处于同一连通分量之中[^3]。 - 边权重的设定直接影响最终结果的好坏程度,因此务必按照实际物理意义合理赋值。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值