AOJ -0189 Convenient Location && poj 2139 Six Degrees of Cowvin Bacon (floyed求任意两点间的最短路)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=78207
看懂题就好。
求某一办公室到其他办公室的最短距离。
多组输入,n表示n条关系,下面n次每次输入 x y d表示x到y的距离是d。
输出办公室的编号和距离。
因为 顶点数小于10,直接floyed求出所有距离之后,枚举出最短距离即可。
/* ***********************************************
Author : zch
Created Time :2015/5/13 12:47:27
File Name :AOJ -0189 Convenient Location
************************************************ */ #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const int maxn = ;
const int INF = 0xfffffff;
int d[maxn][maxn];
int n;
void init()
{
n=;
for(int i=; i<maxn; i++)
for(int j=; j<maxn; j++)
if(i==j) d[i][j]=;
else d[i][j]=INF; }
void floyd()
{
for(int k=; k<n; k++)
for(int i=; i<n; i++)
for(int j=; j<n; j++)
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
} int main()
{
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int m,a,b,c;
while(~scanf("%d",&m)&&m)
{
init();
for(int i=; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
d[a][b]=d[b][a]=c;
n=max(n,max(a,b)+);
}
//printf("%d\n",n);
floyd();
int sum=,id=,cnt=INF;
for(int i=; i<n; i++)
{
sum=;
for(int j=; j<n; j++)
{
sum+=d[i][j];
}
if(sum<cnt)
{
cnt=sum;
id=i;
}
}
printf("%d %d\n",id,cnt);
}
return ;
}
http://poj.org/problem?id=2139
奶牛们在玩一种游戏,游戏的规则是是这样的,奶牛到自身的距离为0,如果两个奶牛合作同一部电影,那么他们的距离为1,如果两只牛没有在一起合作过,但他们都和另一头牛合作过,那么他们距离为2,依次类推,问到其他牛的平均距离最小的多少。 输出最小平均距离乘以100。
做法跟上面一题类似,都是找一个点到其他点的最短距离。注意输出的时候可以先乘以100,这样就不会出错了。
/* ***********************************************
Author : zch
Created Time :2015/5/13 13:50:22
File Name :poj-2139 Six Degrees of Cowvin Bacon
************************************************ */ #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const int maxn = ;
const int INF = <<;
int d[maxn][maxn];
int f[maxn];
int n;
void init()
{
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
if(i==j) d[i][j]=;
else d[i][j]=INF; }
void floyd()
{
for(int k=; k<=n; k++)
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
} int main()
{
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int m,k,a,j;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=; i<m; i++)
{
scanf("%d",&k);
for(int j=;j<k;j++)
{
scanf("%d",&f[j]);
}
for(int x=;x<k;x++)
for(int y=x+;y<k;y++)
d[f[x]][f[y]]=d[f[y]][f[x]]=;
}
//printf("%d\n",n);
floyd();
int sum=,cnt=INF;
for(int i=; i<=n; i++)
{
sum=;
for(int j=; j<=n; j++)
{
sum+=d[i][j];
}
if(sum<cnt)
{
cnt=sum;
}
}
//printf("%d\n",cnt);
printf("%d\n",cnt*/(n-));
}
return ;
}
AOJ -0189 Convenient Location && poj 2139 Six Degrees of Cowvin Bacon (floyed求任意两点间的最短路)的更多相关文章
- 任意两点间最短距离floyd-warshall ---- POJ 2139 Six Degrees of Cowvin Bacon
floyd-warshall算法 通过dp思想 求任意两点之间最短距离 重复利用数组实现方式dist[i][j] i - j的最短距离 for(int k = 1; k <= N; k++) f ...
- POJ 2139 Six Degrees of Cowvin Bacon (floyd)
Six Degrees of Cowvin Bacon Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Ja ...
- <poj - 2139> Six Degrees of Cowvin Bacon 最短路径问题 the cow have been making movies
本题链接:http://poj.org/problem?id=2139 Description: The cows have been making movies lately, so the ...
- POJ 2139 Six Degrees of Cowvin Bacon
水题,Floyd. #include<cstdio> #include<cstring> #include<algorithm> using namespace s ...
- POJ 2139 Six Degrees of Cowvin Bacon (Floyd)
题意:如果两头牛在同一部电影中出现过,那么这两头牛的度就为1, 如果这两头牛a,b没有在同一部电影中出现过,但a,b分别与c在同一部电影中出现过,那么a,b的度为2.以此类推,a与b之间有n头媒介牛, ...
- POJ 2139 Six Degrees of Cowvin Bacon (弗洛伊德最短路)
题意:奶牛拍电影,如果2个奶牛在同一场电影里演出,她们的合作度是1,如果ab合作,bc合作,ac的合作度为2,问哪一头牛到其他牛的合作度平均值最小再乘100 思路:floyd模板题 #include& ...
- POJ:2139-Six Degrees of Cowvin Bacon
传送门:http://poj.org/problem?id=2139 Six Degrees of Cowvin Bacon Time Limit: 1000MS Memory Limit: 6553 ...
- AOJ 0189 Convenient Location (Floyd)
题意: 求某一个办公室 到其他所有办公室的 总距离最短 办公室数 不超过10 输入:多组输入,每组第一行为n (1 ≤ n ≤ 45),接下来n行是 (x, y, d),x到y的距离是d输出:办公室 ...
- AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)
题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C All Pairs Shortest Path Input ...
随机推荐
- angularJs 问题
1. IE不能渲染指令中的 style="background-color",而chrome和firefox可以 <!DOCTYPE html> <html ng ...
- 编程计算并输出1~n之间所有素数之和
http://www.tuicool.com/articles/qaaA3i TODO
- 常用的Python字符串常量
下面是一些常用的Python字符串常量string.digits:包含0-9的字符串string.letters:包含所有大小写字母的字符串 string.lowercase:所有小写字母string ...
- .NET设计模式(15):结构型模式专题总结(转)
摘要:结构型模式,顾名思义讨论的是类和对象的结构,它采用继承机制来组合接口或实现(类结构型模式),或者通过组合一些对象,从而实现新的功能(对象结构型模式).这些结构型模式,它们在某些方面具有很大的相似 ...
- 持久化消息队列memcacheq的安装配置
MemcacheQ 是一个基于 MemcacheDB 的消息队列服务器. 一.memcacheq介绍 特性: 1.简单易用 2.处理速度快 3.多条队列 4.并发性能好 5.与memcache的协议兼 ...
- samsung-smart app 开发
http://www.samsungdforum.com/ http://seller.samsungapps.com/login/signIn.as?returnURL=%2fmain%2fsell ...
- 图解 javascript 作用域链
还是之前那一段简单的javascript代码: window.onload=function(){ function sub(a,b){ return a-b; } var result=sub(10 ...
- LA 3713
The Bandulu Space Agency (BSA) has plans for the following three space missions: Mission A: Landing ...
- ZOJ 2677 Oil Deal(最大生成树)
题意:破坏石油管道,现一直破坏各个管道所要付出的代价,问在有一定money并且要保证剩余的管道为生成树的情况下, 最多能破坏多少个管道,并将他们的编号从小到大输出来 思路:将边从大到小排序,构造生成树 ...
- php获取网页header信息的4种方法
php获取网页header信息的方法多种多样,就php语言来说,我知道的方法有4种, 下面逐一献上. 方法一:使用get_headers()函数 推荐指数: ★★★★★ get_header方法最简单 ...