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. birt报表中使用多个数据集。

    这个问题困扰了几天,也没搜到答案,由于工作需要,创建了两个数据集和两个表格,第一个数据集和表格之间没有任何问题.但是第二个数据集拖过去就显示不可用,除非拖到表格外面,当然也就没用了.一朋友说拖一个网格 ...

  2. 给div命名,使逻辑更加清晰

    在上一小节中,我们把一些标签放进<div>里,划分出一个独立的逻辑部分.为了使逻辑更加清晰,我们可以为这一个独立的逻辑部分设置一个名称,用id属性来为<div>提供唯一的名称, ...

  3. JavaScript--模拟验证码

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. Taxi Cab Scheme POJ && HDU

    Online Judge Problem Set Authors Online Contests User Web Board Home Page F.A.Qs Statistical Charts ...

  5. js与jquery获取父元素,删除子元素的不同方法

    var obj=document.getElementById("id");得到的是dom对象,对该对象进行操作的时候使用js方法 var obj=$("#id" ...

  6. 【转】.NET开发者必备的11款免费工具

    摘要:本文介绍一些最适合.NET开发人员简化Web开发的最佳工具,这些工具都是开源的,有的开发工具你既可以从网上下载可执行文件,也可以下载源代码.一些工具拥有可扩展的框架,是一个持续集成工具. 原文链 ...

  7. php设计模式之简单工厂模式

    ①抽象基类:类中定义抽象一些方法,用以在子类中实现 ②继承自抽象基类的子类:实现基类中的抽象方法 ③工厂类:用以实例化所有相对应的子类 /** * * 定义个抽象的类,让子类去继承实现它 * */ a ...

  8. [解决]UserLibrary中的jar包不会自动发布Tomcat的lib目录下(基于MyEclipse2014)

    1.在工程名称上单击[右键] —— 单击[Properties]选项,点击后会弹出属性窗口: 2.选择[Properties]后在左侧树中找到[MyEclipse] —— [Deployment As ...

  9. Xcode can't verify the identity of the server

    当升级了苹果系统到 OS X El Captain 之后  ,打开Xcode 有时候会报错 如图 而且打开 svn  也会出类似错误  点击continue  了  下次 还会 出现 .这个很好解决 ...

  10. tomcat原理

    1 - Tomcat Server的组成部分 1.1 - Server A Server element represents the entire Catalina servlet containe ...