Risk is a board game in which several opposing players attempt to conquer the world. The gameboard consists of a world map broken up into hypothetical countries. During a player's turn, armies stationed in one country are only allowed to attack only countries with which they share a common border. Upon conquest of that country, the armies may move into the newly conquered country.

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

The following connection diagram illustrates the sample input.

Input

Input to your program will consist of a series of country configuration test sets. Each test set will consist of a board description on lines 1 through 19. The representation avoids listing every national boundary twice 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 indicating how many "higher-numbered" countries share borders with country I, then X distinct integers J greater than I and not exceeding 20, each describing a boundary between countries I and J. Line 20 of the test set contains a single integer (1 <= N <= 100) indicating the number of country pairs that follow. 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; your program should continue reading and processing until reaching the end of file. There will be at least one path between any two given countries in every country configuration.

Output

For each input set, your program should print the following message "Test Set #T" where T is the number of the test set starting with 1. The next NT lines each will contain the result for the corresponding test in the test set - that is, the minimum number of countries to conquer. The test result line should contain the start country code A the string " to " the destination country code B ; the string ": " and a single integer indicating the minimum number of moves required to traverse from country A to country B in the test set. Following all result lines of 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 

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

题意:前19第i行先给出i与n个城市连通,再给出这n个城市的序号,连通是双向的

然后每给出两个点,求距离

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int inf = 999999999;
int map[30][30]; void floyd()
{
int i,j,k;
for(k = 1; k<=20; k++)
for(i = 1; i<=20; i++)
for(j = 1; j<=20; j++)
{
if(map[i][j]>map[i][k]+map[k][j])
map[i][j] = map[i][k]+map[k][j];
}
} int main()
{
int i,j,n,x,y,cas = 1;
while(~scanf("%d",&n))
{
for(i = 0; i<=22; i++)
{
for(j = 0; j<=22; j++)
map[i][j] = inf;
}
for(i = 1; i<=n; i++)
{
scanf("%d",&x);
map[1][x] = map[x][1] = 1;
}
for(i = 2; i<=19; i++)
{
scanf("%d",&n);
while(n--)
{
scanf("%d",&x);
map[i][x] = map[x][i] = 1;
}
}
floyd();
printf("Test Set #%d\n",cas++);
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&x,&y);
printf("%d to %d: %d\n",x,y,map[x][y]);
}
printf("\n");
} return 0;
}

ZOJ1221 && UVA567:Risk(Floyd)的更多相关文章

  1. 最短路径:Dijkstra & Floyd 算法图解,c++描述

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. HDU 4034 Graph:反向floyd

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 有一个有向图,n个节点.给出两两节点之间的最短路长度,问你原图至少有多少条边. 如果无解 ...

  3. BZOJ 1641 [Usaco2007 Nov]Cow Hurdles 奶牛跨栏:新版floyd【路径上最大边最小】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1641 题意: 给你一个有向图,n个点(n <= 300),m条边,边权为h[i]. ...

  4. [C++]多源最短路径(带权有向图):【Floyd算法(动态规划法)】 VS n*Dijkstra算法(贪心算法)

    1 Floyd算法 1.1 解决问题/提出背景 多源最短路径(带权有向图中,求每一对顶点之间的最短路径) 方案一:弗洛伊德(Floyd算法)算法 算法思想:动态规划法 时间复杂度:O(n^3) 形式上 ...

  5. uva oj 567 - Risk(Floyd算法)

    /* 一张有20个顶点的图上. 依次输入每个点与哪些点直接相连. 并且多次询问两点间,最短需要经过几条路才能从一点到达另一点. bfs 水过 */ #include<iostream> # ...

  6. poj 1125 (floyd)

    http://poj.org/problem?id=1125. 题意:在经纪人的圈子里,他们各自都有自己的消息来源,并且也只相信自己的消息来源,他们之间的信息传输也需要一定的时间.现在有一个消息需要传 ...

  7. UVA 247 电话圈(Floyd传递闭包+输出连通分量)

    电话圈 紫书P365 [题目链接]电话圈 [题目类型]Floyd传递闭包+输出连通分量 &题解: 原来floyd还可以这么用,再配合连通分量,简直牛逼. 我发现其实求联通分量也不难,就是for ...

  8. [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  9. Wikioi 1020 孪生蜘蛛 Label:Floyd最短路

    题目描述 Description 在G城保卫战中,超级孪生蜘蛛Phantom001和Phantom002作为第三层防卫被派往守护内城南端一带极为隐秘的通道. 根据防护中心的消息,敌方已经有一只特种飞蛾 ...

随机推荐

  1. CSS实现背景透明而背景上的文字不透明完美解决

    在我们设计制作一些网页的时候可能会用到半透明的效果,首先我们可能会想到用PNG图片处理,当然这是一个不错的办法,唯一的兼容性问题就是ie6 下的BUG,但这也不困难,加上一段js处理就行了.但假如我们 ...

  2. maven指定构建的编码格式

    pom.xml文件添加如下内容: <properties>    <project.build.sourceEncoding>UTF-8</project.build.s ...

  3. Web.Config文件中添加数据库配置文件

    1获取所有配置文件节点的类ConfigurationManager 2数据库节点<ConnectionStrings> <add> name ="Sqlconnect ...

  4. phonegap 2.8.1 toast

    目录结构如下: 以上三个用红色框勾出的地方是需要修改的文件夹. 首先:添加java代码. 在src目录下新建一个包裹:org.apache.cordova 在该包裹下新建类:ToastPlugin.j ...

  5. PHPexcel数据按模板导出

    <?php header("Content-type: text/html; charset=gb2312"); error_reporting(E_ALL); ini_se ...

  6. ViewPager 嵌套Listview 让Listview响应 ViewPager 左右滑事件

    一段拦截判断而已.   之前一直误解了一个拦截的描述.导致搞了半天. 结论: onInterceptTouchEvent 返回true,就由本身View的onTouchEvent进行事件消费. /** ...

  7. Oracle oerr使用

    [oracle@cuug ~]$ oerr ora 01555 01555, 00000, "snapshot too old: rollback segment number %s wit ...

  8. Hibernate 持久化对象的状态

    持久化对象有3种状态:1.持久化状态      2.临时状态      3.游离状态 Session 的特定方法能使对象从一个状态转换到另一个状态临时对象(transient)•    在使用代理主键 ...

  9. Thinkphp Ajax传地址

    在使用文本编辑器时,如果加入图片,涉及到图片的src,需要用到Ajax传地址到处理页面. 在使用Ajax的过程中,如果要通过JSON传递路径值到处理页面,会出现传值不正确. 解决方法就是在传值之前将路 ...

  10. table表格cellspacing与cellpadding属性

    cellspacing属性 用来指定表格各单元格之间的空隙. cellpadding属性 用来指定单元格内容与单元格边界之间的空白距离的大小. 此属性的参数值也是数字,表示单元格内容与上下边界之间空白 ...