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. Netty 对通讯协议结构设计的启发和总结

    Netty 通讯协议结构设计的总结 key words: 通信,协议,结构设计,netty,解码器,LengthFieldBasedFrameDecoder 原创 包含与机器/设备的通讯协议结构的设计 ...

  2. C++ Templates之模板元编程

    #ifndef POW3_H #define POW3_H template <int N> class Pow3 { public: enum{result = 3 * Pow3< ...

  3. BZOJ 1029: [JSOI2007]建筑抢修 贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1029 小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落 ...

  4. BZOJ1500 维修数列

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=1500 [前言] 据说没打这题就相当于没打过Splay,这题简直就是让你内心崩溃的... 这 ...

  5. 【BZOJ】【3790】神奇项链

    Manacher算法/DP 找出所有的回文串,看做是一个个线段,那么问题就转化成了用最少的线段将整个区间覆盖起来,可以重叠,那么这就是一个DP了= = Orz ZKY大爷,让蒟蒻开眼界了……头一次知道 ...

  6. nodeJs 初探 ~

    今天晚上,开始时间了一下nodejs,跟着 Node入门 一步步的往下走.对node开发也有了初步的了解. 期间没有碰到什么问题,只有在最后的时候,碰到了几个问题.在这里记录一下: 1 . cross ...

  7. [百度空间] [转]内存屏障 - MemoryBarrier

    处理器的乱序和并发执行 目前的高级处理器,为了提高内部逻辑元件的利用率以提高运行速度,通常会采用多指令发射.乱序执行等各种措施.现在普遍使用的一些超标量处理器通常能够在一个指令周期内并发执行多条指令. ...

  8. 研究AFIncrementalStore

    一.增量存贮(AFIncrementalStore:NSIncrementalStore)在CoreData中所处位置

  9. HDU 4572 Bottles Arrangement(找规律,仔细读题)

    题目 //找规律,123321123321123321…发现这样排列恰好可以错开 // 其中注意题中数据范围: M是行,N是列,3 <= N < 2×M //则猜测:m,m,m-1,m-1 ...

  10. POJ 1507

    #include<iostream> #include<stdio.h> using namespace std; #include<iomanip> double ...