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 ...
随机推荐
- sql server 数据页缓冲区的内存瓶颈分析
查看数据库的计数器: SELECT * FROM sys.dm_os_performance_counters **也可以使用系统的性能计监测器查看. 右键图表-> 添加计数器. ...
- ASP.NET和JSP相似方法总结(持续中。。)
一.HTTP请求处理 1.获取GET请求数据 ASP.NET:Request.QueryString[name] JSP:request.getParameter(String name); 2.解决 ...
- SPL 全面剖析
SPL 全面剖析 来自百度百科 http://baike.baidu.com/view/1130234.htm?fr=aladdin SPL(Standard PHP Library) IN PHP5 ...
- iOS导航栏-导航栏透明
设置一张透明图片:nav_bargound.png //导航栏背景 [self.navigationController.navigationBar setBackgroundImage:[ ...
- Ms sql server sql优化技巧
SET STATISTICS PROFILE ON SET STATISTICS IO ON SET STATISTICS TIME ON SELECT * FROM userinfo WHERE N ...
- c# string.Format用法总结
文章出处:http://www.cnblogs.com/7788/archive/2009/05/13/1455920.html 先举几个简单的应用案例: 1.格式化货币(跟系统的环境有关,中文系统默 ...
- Shell实现跳板机,为什么用跳板机
整理自:http://blog.chinaunix.net/uid-22101889-id-3167454.html 注意:请谨慎使用,到现在为止,使用了,我还没找到改回去的方法. 1. 问题 ...
- js小技巧(一)
事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture(); event.srcE ...
- QTP10补丁汇总
QTP10补丁汇总 QTP_00591.EXE QTP10 调试器视图问题的补丁 QTP_00591 - Prevent QuickTest Debug Viewer Problems when Pr ...
- iis 重启 (三种方法)
iis 重启 (三种方法) WINDOWS提供WEB服务的IIS有时候会出现访问过大导致网站打不开,这时重启IIS是最好的选择. 方法/步骤 1 1.界面操作 打开“控制面板”->“管理工具”- ...