Zoj 1221 Risk Shortest circuit

Source: Internet
Author: User
Tags cas

Description Risk is a board game in which several opposing players attempt to conquer the world. The gameboard consists of a world map broken to hypothetical countries. During a player ' s turn, armies stationed in one country is only allowed to attack only countries with which they share a Common border. Upon conquest of that country, the armies could 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 all countries between 1 and connections to other countries, your TAS K is to write a function that takes a starting country and a destination country and computes the minimum number of countr IES that must is conquered to reach the destination. Need to output the sequence of countries, just the number of countries to be conquered including the Destinatio N. For example, if starting and destination countries is neighbors, then your program should return one.

The following connection diagram illustrates the sample input.


Input

Input to your program would consist of a series of country configuration test sets. Each test set would 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 am less than, contains a integer X indicating how many "higher-numbered" countries share B Orders with Country I, then X distinct integers J greater than I and not exceeding, each describing a boundary between Countries I and J. Line 1 of the test set contains a single integer (<= N <=) indicating the number of countr Y pairs that follow. The next N lines each contain exactly the integers (1 <= A, B <= 20; A!=B) indicating the starting and ending countries for a possible conquest.

There can multiple test sets in the input; Your program should continue reading and processing until reaching the end of file. There'll is at least one path between any and 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 St Arting with 1. The next NT lines each would 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 and 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
0 {
19 5
18 19
16 20


Sample Output

Test Set #1
1 to 20:7
2 to 9:5
5:6
19:2
20:2 to



Dijkstra

#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace

Std

const int inf=10000000;
int map[22][22];
int dis[22];

int vis[22];
    void Dijkstra (int s) {int i,j,k,mn;
    for (i=1;i<=20;i++) dis[i]=map[s][i],vis[i]=0;
    Vis[s]=1;
        for (I=1; i<=20;i++) {k=s;
        Mn=inf;
        for (j=1;j<=20;j++) if (!VIS[J]&AMP;&AMP;DIS[J]&LT;MN) k=j,mn=dis[j];
        Vis[k]=1; for (j=1;j<=20;j++) if (!vis[j]&& (Map[k][j]+dis[k]) <dis[j]) dis[j]=dis[k]+map[k][j
    ];
    }} int main () {int i,j,k,cas=1;
        while (scanf ("%d", &k)!=eof) {int m,a,b;
            for (i=1;i<=20;i++) for (j=1;j<=20;j++) if (i==j) map[i][j]=0;
        else Map[i][j]=inf;
            for (j=0;j<k;j++) {scanf ("%d", &a);
 Map[1][a]=map[a][1]=1;       } for (i=2;i<20;i++) {scanf ("%d", &k);
            for (j=0;j<k;j++) {scanf ("%d", &a);
          Map[i][a]=map[a][i]=1;
                 }}/* for (i=1;i<=19;i++) {for (j=1;j<=19;j++) {
             cout<<map[i][j]<< "";
         } cout<<endl;
        }*/scanf ("%d", &m);
        printf ("Test Set #%d\n", cas++);
            while (m--) {scanf ("%d%d", &a,&b);
            Dijkstra (a);
    printf ("%d to%d:%d\n", a,b,dis[b]);//This space makes me wa three times} printf ("\ n");
} return 0;
 }


Floyd
#include <cstdio> #include <cstring> #include <algorithm> int g[22][22];

int n;
    BOOL Init () {int num;
                for (int i = 1;i <= 20;i + +) {for (int j = 1;j <= 20;j + +) {if (i = = j)
            G[I][J] = 0;
        else g[i][j] = 1000000;
        }} for (int i = 1; I <= 19;i + +) {if (scanf ("%d", &num)! = 1) return false;
            for (int j = 0;j < Num;j + +) {int A;
            scanf ("%d", &a);
        G[i][a] = g[a][i] = 1;
}} return true; } void Floyd () {for (int i = 1;i <= 20;i + +) {for (int j = 1;j <= 20;j + +) {fo
            R (int k = 1;k <= 20;k + +) {G[j][k] = Std::min (G[j][k], G[j][i] +g[i][k]);
    }}}} int main () {int cas = 1;
        while (init ()) {Floyd ();
        scanf ("%d", &n);
      int A, B;  printf ("Test Set #%d\n", CAS + +);
            for (int i = 1;i <= n;i + +) {scanf ("%d%d", &a,&b);
        printf ("%2d to%2d:%d\n", a,b,g[a][b]);

    } printf ("\ n");
} return 0; }


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.