poj1603(最短路)
来源:互联网 发布:淘宝流量钱包流量充值 编辑:程序博客网 时间:2024/06/11 08:21
Risk
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 2922 Accepted: 1357
Description
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 first sample input.
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 first 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 file; 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.
There can be multiple test sets in the input file; 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 32 3 43 4 5 61 61 72 12 131 82 9 101 111 112 12 171 142 14 152 15 161 161 192 18 191 201 2051 202 919 518 1916 20
Sample Output
Test Set #11 to 20: 72 to 9: 519 to 5: 618 to 19: 216 to 20: 2
解题思路:弗洛伊德求最短路
AC代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define INF 0x3f3f3fint dis[101][101];void init(int n){int i,j;for(i=0;i<=n;i++){for(j=0;j<=n;j++){if(i==j) dis[i][j]=dis[j][i]=0;elsedis[i][j]=INF;}}}void flyod(int n){int i,j,k;for(i=1;i<=n;i++){ for(j=1;j<=n;j++) { for(k=1;k<=n;k++) { dis[j][k]=min(dis[j][k],dis[j][i]+dis[i][k]); } }} }int main(){int i,j,k,n,x,ca=1,v;while(scanf("%d",&x)!=EOF){init(20);while(x--){scanf("%d",&v);dis[1][v]=dis[v][1]=1;}i=2;while(i<=19){scanf("%d",&k);for(j=1;j<=k;j++){scanf("%d",&x);dis[i][x]=dis[x][i]=1;}i++;}flyod(20);scanf("%d",&n);printf("Test Set #%d\n",ca++);int a,b; while(n--){scanf("%d%d",&a,&b);printf("%d to %d: %d\n",a,b,dis[a][b]);}printf("\n");}return 0;}
0 0
- poj1603 Floyd最短路
- poj1603(最短路)
- POJ1603
- 最短路 & 次短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 基础控件
- ZooKeeper架构及简介
- BZOJ4170 极光(CDQ分治 或 树套树)
- 项目3:小试循环---(5)求m!
- Java类加载器
- poj1603(最短路)
- DeepLearning--Part2--Chapter6:Feedforward-Deep-Networks(1)
- BigDecimal使用以及异常处理
- n-1 个数中三个数乘积最大的最小公倍数
- 51nod 1347 旋转字符串
- UIActivityViewController头文件学习
- DeepLearning--Part3--Chapter16:Representation Learning(1) categories:
- JAVA帮助文档全系列说明
- sqlite3 笔记整理