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求任意两点间的最短路)的更多相关文章

  1. 任意两点间最短距离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 ...

  2. POJ 2139 Six Degrees of Cowvin Bacon (floyd)

    Six Degrees of Cowvin Bacon Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Ja ...

  3. <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 ...

  4. POJ 2139 Six Degrees of Cowvin Bacon

    水题,Floyd. #include<cstdio> #include<cstring> #include<algorithm> using namespace s ...

  5. POJ 2139 Six Degrees of Cowvin Bacon (Floyd)

    题意:如果两头牛在同一部电影中出现过,那么这两头牛的度就为1, 如果这两头牛a,b没有在同一部电影中出现过,但a,b分别与c在同一部电影中出现过,那么a,b的度为2.以此类推,a与b之间有n头媒介牛, ...

  6. POJ 2139 Six Degrees of Cowvin Bacon (弗洛伊德最短路)

    题意:奶牛拍电影,如果2个奶牛在同一场电影里演出,她们的合作度是1,如果ab合作,bc合作,ac的合作度为2,问哪一头牛到其他牛的合作度平均值最小再乘100 思路:floyd模板题 #include& ...

  7. POJ:2139-Six Degrees of Cowvin Bacon

    传送门:http://poj.org/problem?id=2139 Six Degrees of Cowvin Bacon Time Limit: 1000MS Memory Limit: 6553 ...

  8. AOJ 0189 Convenient Location (Floyd)

    题意: 求某一个办公室 到其他所有办公室的 总距离最短  办公室数 不超过10 输入:多组输入,每组第一行为n (1 ≤ n ≤ 45),接下来n行是 (x, y, d),x到y的距离是d输出:办公室 ...

  9. 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 ...

随机推荐

  1. 【BZOJ】【1503】 【NOI2004】郁闷的出纳员

    Splay Splay的模板题吧……妥妥的序列操作= =(好像有段时间没写过这种纯数据结构题了……) /************************************************ ...

  2. 输出string vector到file

    #include <fstream> #include <iterator> #include <string> #include <vector> i ...

  3. Swift-5-流程控制

    // Playground - noun: a place where people can play import UIKit // For-In 循环 // 1 遍历数字区间 ... { prin ...

  4. TestDriven.Net

    转: http://www.cnblogs.com/AlexLiu/archive/2008/12/01/1345002.html

  5. ios中webservice报文的拼接

    1.报文头需要密码验证的 - (void)sendAsynWebServiceRequest:(NSString *)nameSpace method:(NSString *)method reque ...

  6. HDU 5014Number Sequence

    思路: 对于一个二进制100011: 尽量将填满:填成111111: 然后有一个很好算的方法 gets(n)表示二进制下N有多少位,N^X=(111111)2 X=111111^N; 其实答案可以直接 ...

  7. Long和Date数据类型之间相互转换代码 - 调整时间推前往后,截取long型日期方法。

    SimpleDateFormat DATETIME_SEC_STR = new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateForm ...

  8. MYSQL注入天书之服务器(两层)架构

    Background-6 服务器(两层)架构 首先介绍一下29,30,31这三关的基本情况: 服务器端有两个部分:第一部分为tomcat为引擎的jsp型服务器,第二部分为apache为引擎的php服务 ...

  9. 初步体验javascript try catch机制

    javascript在ECMAScript3中引入了try catch finally机制,大致原理和其他语言一样. 我们也可以自定义错误事件. 但是事先声明:我们自定义的错误事件,只支持对name. ...

  10. JS获取Url参数的通用方法

    //获取URL中的参数 function request(paras) { var url = location.href.replace('#', ''); var paraString = url ...