ZOJ1221 && UVA567:Risk(Floyd)
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
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
题意:前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)的更多相关文章
- 最短路径:Dijkstra & Floyd 算法图解,c++描述
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- HDU 4034 Graph:反向floyd
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 题意: 有一个有向图,n个节点.给出两两节点之间的最短路长度,问你原图至少有多少条边. 如果无解 ...
- BZOJ 1641 [Usaco2007 Nov]Cow Hurdles 奶牛跨栏:新版floyd【路径上最大边最小】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1641 题意: 给你一个有向图,n个点(n <= 300),m条边,边权为h[i]. ...
- [C++]多源最短路径(带权有向图):【Floyd算法(动态规划法)】 VS n*Dijkstra算法(贪心算法)
1 Floyd算法 1.1 解决问题/提出背景 多源最短路径(带权有向图中,求每一对顶点之间的最短路径) 方案一:弗洛伊德(Floyd算法)算法 算法思想:动态规划法 时间复杂度:O(n^3) 形式上 ...
- uva oj 567 - Risk(Floyd算法)
/* 一张有20个顶点的图上. 依次输入每个点与哪些点直接相连. 并且多次询问两点间,最短需要经过几条路才能从一点到达另一点. bfs 水过 */ #include<iostream> # ...
- poj 1125 (floyd)
http://poj.org/problem?id=1125. 题意:在经纪人的圈子里,他们各自都有自己的消息来源,并且也只相信自己的消息来源,他们之间的信息传输也需要一定的时间.现在有一个消息需要传 ...
- UVA 247 电话圈(Floyd传递闭包+输出连通分量)
电话圈 紫书P365 [题目链接]电话圈 [题目类型]Floyd传递闭包+输出连通分量 &题解: 原来floyd还可以这么用,再配合连通分量,简直牛逼. 我发现其实求联通分量也不难,就是for ...
- [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 ...
- Wikioi 1020 孪生蜘蛛 Label:Floyd最短路
题目描述 Description 在G城保卫战中,超级孪生蜘蛛Phantom001和Phantom002作为第三层防卫被派往守护内城南端一带极为隐秘的通道. 根据防护中心的消息,敌方已经有一只特种飞蛾 ...
随机推荐
- OD: Kernel Exploit - 2 Programming
本节接前方,对 exploitme.sys 进行利用. exploitme.sys 存在任意地址写任意内容的内核漏洞,现在采用执行 Ring0 Shellcode 的方式进行利用. 获取 HalDis ...
- UITableView中的visibleCells的用法(visibleCells帮上大忙了)
这两天遇到一个问题,UITableView中需要加入动画,而且每一行的速度不一样. 刚开始做时把所有的cell都遍历一遍加上动画,后来发现,如果数据很多时,就会出现各种各样的问题,而且没有显示在界 ...
- 【转】 iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
原文:http://blog.csdn.net/hmt20130412/article/details/34523235 本来只是打算介绍一下addChildViewController这个方法的,正 ...
- 微信小应用vs progressive-web-apps
https://developers.google.com/web/progressive-web-apps/
- hibernate_validator_03
约束继承 如果要验证的对象继承于某个父类或者实现了某个接口,那么定义在父类或者接口中的约束会在验证这个对象的时候被自动加载,如同这些约束定义在这个对象所在的类中一样. 让我们来看看下面的示例: pac ...
- ubuntu下apache与php配置
实验环境 uname -a Linux tomato 4.4.0-34-generic #53-Ubuntu SMP Wed Jul 27 16:06:39 UTC 2016 x86_64 x86_6 ...
- linux的du和df命令
今天也有同学问我Linux下查看目录大小的命令,现在也将前阵子学习到du/df两个命令总结一下吧.前阵子测试工作中有遇到过由于磁盘空间满导致程序无法执行到情况,所以使用了df和du两个命令. du查看 ...
- Activiti 工作流得到最后一次批注的时间
我们有时候在工作流开发中可能会遇到这样的需求,就是已经审批结束的流程,可能我们还是仍然需要修改业务表的结果,而且我们需要一个时间期限,比如:在5天内可以进行修改 ,这个时候我们就需要得到我们最后一步审 ...
- @Index用法——javax.persistence.Index
package com.springup.utiku.model; import java.io.Serializable; import javax.persistence.Entity; impo ...
- php错误级别设置
在php.ini中可以设置服务器对错误的报警级别.在默认情况下,php将报告除了通知之外的所有错误. 错误报告级别是通过一些预定义的常量来设置的, 语法 int error_reporting ( [ ...