Zoj 1221 Risk "Shortest 3 Ways"

Source: Internet
Author: User
Tags cas time limit cmath

Risk time Limit:2 Seconds Memory limit:65536 KB 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
2 9
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


Test instructions

Direct analysis of the data ~ The first 19 lines is the link between 1~20 points, the next number m indicates that there is M group S, T requires the shortest path to output ~


Dijkstra:

#include <cstdio> #include <cstring> #include <cmath> #include <queue> #define MEM (A, B) memset (a  , (b), sizeof (a)) #define WI (a) while (a--) #define SI (a) scanf ("%d", &a) #define PI (a) printf ("%d\n", (a)) #define INF
0x3f3f3f3f #include <algorithm> using namespace std;
const int MX = 22;
int MAP[MX][MX];
int DIS[MX], vis[mx];
int n;
int S, t;  void Dijkstra (int s) {mem (Vis, 0);
	Vis[s] = 1;
	for (int i = 1; i <=; i++) dis[i] = Map[s][i];
	Dis[s] = 0;
	int I, j, K;
		for (i = 1; i <; i++) {int minn = INF;
				for (j = 1; J <=; J + +) {if (!vis[j] && minn > Dis[j]) {minn = dis[j];
			K = J;
		}} Vis[k] = 1;
		for (j = 1; J <=; J + +) {if (!vis[j]) dis[j] = min (Dis[j], dis[k]+map[k][j]);
	}}} int main () {int cas = 1;
		while (Si (n) ==1) {mem (map, 0x3f);
		int A, B;
		Wi (n) {Si (a); Map[1][a] = map[a][1] = 1;  
			} for (int i = 2; I <=; i++) {Si (a); Wi (a) {Si (b); Map[b][i] = map[i][b] = 1;
		}} printf ("Test Set #%d\n", cas++);  int m;
		Si (m);
			Wi (m) {scanf ("%d%d", &s, &t);
			Dijkstra (s);
		printf ("%d to%d:%d\n", S, T, Dis[t]);
	} printf ("\ n");
} return 0; }

Floyd

 #include <cstdio> #include <cstring> #include <cmath> #include <queue> # Define MEM (A, B) memset (A, (b), sizeof (a)) #define WI (a) while (a--) #define SI (a) scanf ("%d", &a) #define PI (a) printf
("%d\n", (a)) #define INF 0x3f3f3f3f #include <algorithm> using namespace std;
const int MX = 22;
int MAP[MX][MX];
int DIS[MX], vis[mx];

int n;
	void Floyd () {int I, j, K; for (k = 1; k <=; k++) {for (i = 1; i <=; i++) {for (j = 1; J <=; j + +) Map[i][j] = min (Map[i][j],	
		MAP[I][K]+MAP[K][J]);
	}}} int main () {int cas = 1;
		while (Si (n) = = 1) {mem (map, 0x3f);
		int A, B;
		Wi (n) {Si (a); Map[1][a] = map[a][1] = 1;
			} for (int i = 2; i <; i++) {Si (a);   Wi (a) {Si (b);
			MAP[I][B] = map[b][i] = 1;
		}} Floyd ();
		printf ("Test Set #%d\n", cas++);  int m;
		Si (m);
		int S, t;
			Wi (m) {scanf ("%d%d", &s, &t);
		printf ("%d to%d:%d\n", S, T, Map[s][t]);
	} printf ("\ n");
} return 0; }

SPFA:

#include <cstdio> #include <cstring> #include <cmath> #include <queue> #define MEM (A, B) memset (a  , (b), sizeof (a)) #define WI (a) while (a--) #define SI (a) scanf ("%d", &a) #define PI (a) printf ("%d\n", (a)) #define INF
0x3f3f3f3f #include <algorithm> using namespace std;
const int MX = 22;
int DIS[MX], vis[mx];
int n, Edgenum;
int S, t;
int head[mx*100];
struct node{int from, to, Val, next;};
Node edge[mx*1000];
	void Init () {edgenum = 0;
Mem (Head,-1);
	} void Add (int u, int v, int w) {node E = {u, V, W, Head[u]};
	Edge[edgenum] = E;
Head[u] = edgenum++;  } void Spfa (int s) {mem (Vis, 0);
	Mem (DIS, 0x3f);
	Queue<int> Q;
	Vis[s] = 1;
	Dis[s] = 0;
	Q.push (s);
		while (!q.empty ()) {int u = q.front (); Q.pop (); vis[u] = 0;
			for (int i = head[u]; i =-1; i = edge[i].next) {int v = edge[i].to;
				if (Dis[v] > Dis[u]+edge[i].val) {dis[v] = Dis[u] + edge[i].val;
					if (!vis[v]) {vis[v] = 1;
				Q.push (v); }}}} printf ("%d to%D:%d\n ", S, T, Dis[t]);
	} int main () {int cas = 1;
		while (Si (n) = = 1) {init ();
		int A, B; 
			Wi (n) {Si (a);
			Add (1, a, 1);
		Add (A, 1, 1);
			} for (int i = 2; i <; i++) {Si (a);
				Wi (a) {Si (b);
				Add (i, B, 1);
			Add (b, I, 1);
		}} printf ("Test Set #%d\n", cas++);  int m;
		Si (m);
			Wi (m) {scanf ("%d%d", &s, &t);
		SPFA (s);
	} 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.