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 ...
随机推荐
- Navicat Premium 11.0.19中文破解版 安装
一.navicat-premium简介 它是一款可连接多种数据库的软件,具体参见官网介绍:http://www.navicat.com.cn/products/navicat-premium 二.下载 ...
- 【POJ】【1635】Subway Tree Systems
树的最小表示法 给定两个有根树的dfs序,问这两棵树是否同构 题解:http://blog.sina.com.cn/s/blog_a4c6b95201017tlz.html 题目要求判断两棵树是否是同 ...
- Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal
原题地址 二叉树基本操作 [ ]O[ ] [ ][ ]O 代码: TreeNode *restore(vector<i ...
- ios 百度地图
百度地图 中的注意事项 1. 百度地图中 使用了c++ 设置buidSeting compoileSource 为 Object-C++ 强制使用oc++编译器 2. 设置 BuidSeti ...
- Gitlab 7.12 发布 SAML支持及其他更多功能
官方文章:https://about.gitlab.com/2015/06/22/gitlab-7-12-released/ CSDN翻译文章:http://geek.csdn.net/news/de ...
- LSTM/RNN的应用Case
作者:许铁-巡洋舰科技链接:https://www.zhihu.com/question/37082800/answer/126430702来源:知乎著作权归作者所有,转载请联系作者获得授权. 作者: ...
- Android activity属性
android:allowTaskReparenting 是否允许activity更换从属的任务,比如从短信息任务 切换到浏览器任务. android:alwaysRetainTaskState 是否 ...
- 神器——Chrome开发者工具(一)
这里我假设你用的是Chrome浏览器,如果恰好你做web开发,或者是比较好奇网页中的一些渲染效果并且喜欢折腾,那么你一定知道Chrome的开发者工具了.其实其他浏览器也有类似工具,比如Firefox下 ...
- 编程实现linux下的shell
/************************************************************************* > File Name: Kris_shel ...
- mysql之视图
视图 视图是虚拟的表.与包含数据的表不一样,视图只包含使用时动态检索数据的查询. 理解视图最好的办法就是来看一下例子: SELECT cust_name , cust_contact FRO ...