Ultraviolet A 567-risk (Floyd)

Source: Internet
Author: User

Link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 24 & page = show_problem & problem = 508

Question:

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 shoshould return one.

The following connection dimo-strates 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 shocould 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 shocould 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 nextNTLines 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 shocould
Contain the start Country CodeARight-justified in columns 1 and 2; the string''To"In columns 3 to 6; the destination country codeBRight-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 countryATo countryBIn the test set left-justified starting in column 11. Following all result lines of each input set, your program shocould 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

Analysis and Summary:

Obvious Floyd algorithm, on Template

Code:

#include<cstdio>#include<iostream>using namespace std;const int N = 25;const int INF = 1000000000;int d[N][N], n;inline void read_graph(){    for(int i=1; i<N; ++i){        d[i][i] = 0;        for(int j=i+1; j<N; ++j)            d[i][j]=d[j][i]=INF;    }    int a;    for(int i=0; i<n; ++i){        scanf("%d",&a);        d[1][a]=d[a][1]=1;    }    for(int i=2; i<=19; ++i){        scanf("%d",&n);        for(int j=0; j<n; ++j){            scanf("%d",&a);            d[i][a]=d[a][i]=1;        }    }}void Floyd(){    for(int k=1; k<=20; ++k){        for(int i=1; i<=20; ++i){            for(int j=1; j<=20; ++j)                if(d[i][k]!=INF && d[k][j]!=INF){                    d[i][j] = min(d[i][j], d[i][k]+d[k][j]);                }        }    }}inline void output(){    scanf("%d",&n);    int u,v;    for(int i=0; i<n; ++i){        scanf("%d%d",&u,&v);        printf("%2d to %2d: %d\n",u,v,d[u][v]);    }    printf("\n");}int main(){    int cas=1;    while(scanf("%d",&n)!=EOF){        read_graph();        Floyd();        printf("Test Set #%d\n", cas++);        output();    }    return 0;}

-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 ,
D_double (reprinted please mark)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.