poj1603 floyd算法入门

Risk is a board game in which several opposing playersattempt to conquer the world. The gameboard consists of a world map broken upinto hypothetical countries. During a player‘s turn, armies stationed in onecountry are only allowed to attack
only countries with which they share acommon border. Upon conquest of that country, the armies may move into thenewly conquered country.

During the course of play, a player often engages in a sequence of conquestswith the goal of transferring a large mass of armies from some starting countryto a destination country. Typically, one chooses the intervening countries soas to minimize the total
number of countries that need to be conquered. Given adescription of the gameboard with 20 countries each with between 1 and 19connections to other countries, your task is to write a function that takes astarting country and a destination country and computes
the minimum number ofcountries that must be conquered to reach the destination. You do not need tooutput the sequence of countries, just the number of countries to be conqueredincluding the destination. For example, if starting and destination countriesare
neighbors, then your program should return one.

The following connection diagram illustrates the first sample input.

Input

Input to your program will consist of a series of countryconfiguration test sets. Each test set will consist of a board description onlines 1 through 19. The representation avoids listing every national boundarytwice by only listing the fact
that country I borders country J when I < J.Thus, the Ith line, where I is less than 20, contains an integer X indicatinghow many "higher-numbered" countries share borders with country I,then X distinct integers J greater than I and not exceeding 20, each
describinga boundary between countries I and J. Line 20 of the test set contains a singleinteger (1 <= N <= 100) indicating the number of country pairs thatfollow. The next N lines each contain exactly two integers (1 <= A,B <=20; A!=B) indicating the starting
and ending countries for a possible conquest.

There can be multiple test sets in the input file; your program should continuereading and processing until reaching the end of file. There will be at leastone path between any two given countries in every country configuration.

Output

For each input set, your program should print thefollowing message "Test Set #T" where T is the number of the test setstarting with 1. The next NT lines each will contain the result for thecorresponding test in the test set - that is, the minimum
number of countriesto conquer. The test result line should contain the start country code A thestring " to " the destination country code B ; the string ":" and a single integer indicating the minimum number of moves required totraverse from country A to country
B in the test set. Following all result linesof each input set, your program should print a single blank line.

Sample Input

1 3

2 3 4

3 4 5 6

1 6

1 7

2 12 13

1 8

2 9 10

1 11

1 11

2 12 17

1 14

2 14 15

2 15 16

1 16

1 19

2 18 19

1 20

1 20

5

1 20

2 9

19 5

18 19

16 20

Sample Output

Test Set #1

1 to 20: 7

2 to 9: 5

19 to 5: 6

18 to 19: 2

16 to 20: 2

题目分析:题意给出国家间的邻接关系,求从一个国家到另一个国家所经过的最少国家数。构建图每条边权为1,然后用floyd算法即可。另也可以用bfs。

Floyd算法用于求最短路径。三个for循环就可以解决问题,所以它的时间复杂度为O(n^3)。

Floyd算法的基本思想如下:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B。所以,我们假设Dis(AB)为节点A到节点B的最短路径的距离,对于每一个节点X,我们检查Dis(AX) + Dis(XB) < Dis(AB)是否成立,如果成立,证明从A到X再到B的路径比A直接到B的路径短,我们便设置Dis(AB)= Dis(AX) + Dis(XB),这样一来,当我们遍历完所有节点X,Dis(AB)中记录的便是A到B的最短路径的距离。

AC代码:

#include <iostream>

#include <cstdio>

#include <cstring>

using namespace std;

int cost[25][25];         //图的权

int main()

{

int n,m,ca=1;

int a,t;

while(~scanf("%d",&a)){

memset(cost,11,sizeof(cost));

for(int i=1;i<=a;++i){

scanf("%d",&t);

cost[1][t]=cost[t][1]=1;

}

for(int i=2;i<=19;++i){

scanf("%d",&t);

for(int j=1;j<=t;++j){

int x;

scanf("%d",&x);

cost[i][x]=cost[x][i]=1;

}

}

for(int k=1;k<=20;++k)  //floyd

for(int i=1;i<=20;++i)

for(int j=1;j<=20;++j)

cost[i][j]=min(cost[i][k]+cost[k][j],cost[i][j]);

scanf("%d",&m);

printf("Test Set #%d\n",ca++);

for(int i=0;i<m;++i){

int a,b;

scanf("%d%d",&a,&b);

printf("%d to %d: %d\n",a,b,cost[a][b]);

}

printf("\n");

}

return 0;

}

时间: 2024-11-08 10:56:28

poj1603 floyd算法入门的相关文章

ACM: POJ 3660 Cow Contest - Floyd算法

链接 Cow Contest Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Eac

最短路径Dijkstra算法和Floyd算法整理、

转载自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html 最短路径—Dijkstra算法和Floyd算法 Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Dijkstra算法是很有代表性的最短路径算法,在很多专业课程中都作为基本内容有详细的介绍,如数据结构,图论,运筹

最短路径——Floyd算法

如何求一张图中任意两顶点之间的最短路径长度,这里写一种最简单的算法——Floyd算法: 1 #include<stdio.h> 2 3 #define inf 9999 4 5 int main() 6 { 7 int e[10][10]; //用邻接矩阵表示图 8 printf("请输入顶点和边的数目:"); 9 int n,m; 10 scanf("%d%d",&n,&m); 11 for(int i=0;i<n;i++) 12

HDOJ 1217 Arbitrage(拟最短路,floyd算法)

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5679    Accepted Submission(s): 2630 Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform

44. 蛤蟆的数据结构笔记之四十四弗洛伊德Floyd算法

44. 蛤蟆的数据结构笔记之四十四弗洛伊德Floyd算法 本篇名言:"希望是厄运的忠实的姐妹. --普希金" 我们继续来看下数据结构图中的一个算法,这个算法来自图灵奖得主. 1.  Floyd算法介绍 Floyd算法又称为插点法,是一种用于寻找给定的加权图中多源点之间最短路径的算法.该算法名称以创始人之一.1978年图灵奖获得者.斯坦福大学计算机科学系教授罗伯特·弗洛伊德命名.注意这个可不是心理学的那个弗洛伊德. 是解决任意两点间的最短路径的一种算法,可以正确处理有向图或负权的最短路径

所有顶点之间的最短路径算法:Floyd算法。

Floyd算法的基本思想是:设集合S的初始状态为空,然后依次向集合S中加入顶点 0,1,...,n-1,每次加入一个顶点,用二维数组d保存各条最短路径的长度,其中d[i][j]存放的是顶点i到顶点j的最短路径的长度. 详细的说明: Floyd算法中最重要的办法为二维数组d[i][j],d[i][j]为从i到j中间只经过S中的顶点的.所有可能的路径中的最短路径的长度.如果从i到j通过S中的节点无法联通,则设置d[i][j]为正无穷.可以称d[i][j]存放的是从i到j的 当前最短路径 的长度.而随

最短路径之Floyd算法

Floyd算法又称弗洛伊德算法,也叫做Floyd's algorithm,Roy–Warshall algorithm,Roy–Floyd algorithm, WFI algorithm. Floyd算法是一种在有权图中(有确定的非负的权值,不能存在环路)查找最短路径的算法.该算法的一次简单执行可以找出任意结点之间的最短路径(尽管它没有返回路径的具体信息). 思想: Floyd算法通过比较图中任意两点间所有可能存在的路径长度得到最短路径长度. 我们定义一个函数shortestPath(i,j,

hihoCoder#1089 最短路径&#183;二:Floyd算法

原题地址 感觉Floyd算法比Dijkstra还要简单.. 唯一需要注意的是,初始的距离默认值不要设过大,否则溢出就不好了,根据题意,只要大于10^3即可 代码: 1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 6 #define MAX_POINT 1024 7 #define MAX_EDGE 16384 8 9 int N, M; 10 int d[MAX_POINT][MAX_POI

hihocoder1089 Floyd算法

题目链接:http://hihocoder.com/problemset/problem/1089 算法描述: floyd算法是求解图中任意两点最短路的经典算法,复杂度为O(n^3).虽然我们完全可以用n次dijkstra算法来求任意两点的最短路,复杂度也是O(N^3),但如果有一个算法只需要5行代码就可以完成我们想做的事情,我们有什么理由不用它?! floyd算法主要用了动态规划的思想: 设d[i][j]表示i到j的最短路径的长度, 那么我们可以这样来求解d[i][j]: (1)首先,i到j的