floyed多源最短路--Uva567
来源:互联网 发布:数据耦合的例子 编辑:程序博客网 时间:2024/05/11 18:24
Risk
Risk
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.
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 ( ) indicating the number of country pairs that follow. The next N lines each contain exactly two integers () 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.
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 (left-justified starting in column 11).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 right-justified in columns 1 and 2; the string `` to " in columns 3 to 6; the destination country code B right-justified in columns 7 and 8; the string ``: " in columns 9 and 10; and a single integer indicating the minimum number of moves required to traverse from country A to country B in the test set left-justified starting in column 11. 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 204 2 3 5 61 43 4 10 55 10 11 12 19 182 6 72 7 82 9 101 91 102 11 143 12 13 143 18 17 134 14 15 16 170002 18 201 191 2061 208 2015 1611 47 132 16
Sample Output
Test Set #1 1 to 20: 7 2 to 9: 519 to 5: 618 to 19: 216 to 20: 2Test Set #2 1 to 20: 4 8 to 20: 515 to 16: 211 to 4: 1 7 to 13: 3 2 to 16: 4思路:floyed求多元最短路即可,主要是注意输出格式。下面是代码:#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int INF=10000;int grid[50][50];void floyed(){ for(int k=1;k<=20;k++) { for(int i=1;i<=20;i++) { for(int j=1;j<=20;j++) if(i!=j) grid[i][j]=min(grid[i][j],grid[i][k]+grid[k][j]); } }}int main(){ //freopen("in.txt","r",stdin); int x,y; int loop=1; while(cin>>x) { for(int i=1;i<=20;i++) fill(grid[i],grid[i]+21,INF); for(int i=1;i<=x;i++) { scanf("%d",&y); grid[1][y]=grid[y][1]=1; } for(int i=2;i<=19;i++) { scanf("%d",&x); for(int j=1;j<=x;j++) { scanf("%d",&y); grid[i][y]=grid[y][i]=1; } } floyed(); int enq; scanf("%d",&enq); cout<<"Test Set #"<<loop++<<endl; for(int i=1;i<=enq;i++) { scanf("%d%d",&x,&y); printf("%2d to %2d: %d\n",x,y,grid[x][y]); } cout<<endl; } return 0;}
- floyed多源最短路--Uva567
- uva567(最短路 + floyd)
- UVA567
- uva567
- 最短路(Floyed)--poj2139
- floyed最短路+uva10803
- 最短路 (floyed)
- poj1125 floyed 求最短路
- hdu2544最短路(floyed)
- hdu 1599 floyed最短路
- Floyed-Warshall-求最短路
- 【最短路(动态规划)】CODE[VS] 1077 多源最短路 (Floyed模板)
- hdu1690 Bus System floyed最短路
- 最短路--floyed-warshall算法poj1125
- hdu 4034 Graph 最短路Floyed
- POJ1125 Stockbroker Grapevine(最短路Floyed)
- POJ 2240 Arbitrage(最短路Floyed)
- POJ2502 Subway(最短路Floyed)
- usb转串口如何配置?
- NYOJ-733-万圣节派对-2013年11月2日16:27:10
- Android学习.1(线性布局和相对布局)
- Makefile missing separator. Stop排查解决
- 主成分分析
- floyed多源最短路--Uva567
- android 4.x 显示、隐藏导航条
- 【中北大学2013年第一学期新生程序设计大赛 解题报告】
- NYOJ-822-画图-2013年11月2日16:36:34
- how to hide dock of running app on mac
- ASP.NET(C#) VS2010连接Oracle数据库
- 关于自增++a,和a++的相关运算,早年的有意思的笔记
- 集合的划分
- NYOJ-813-对决-2013年11月2日23:16:37