zoj 1221 Risk【最短路 floyd】

ZOJ Problem Set - 1221

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 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 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; 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 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

19 to 5: 6

18 to 19: 2

16 to 20: 2



Source: South Central USA 1997

一下子看到这道题有点蒙,第一英文题读不懂(我英文不好),第二测试数据太长了。。。20+n 行,看了大牛博客后 懂了一些,我采用的是 floyd 方法,对于测试数据怎么输入及怎么处理(放进图)是个问题。

具体的看代码吧。1A:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define min(a,b) a>b?b:a
#define INF 0x3f3f3f3f
int dis[22][22];
void floyd()
{
	int i,j,k;
	for(k = 1; k <= 20; k++)
	for(i = 1; i <= 20; i++)
	for( j = 1; j <=20; j++)
	dis[i][j] = min(dis[i][j], dis[i][k]+dis[k][j]);
}
int main()
{
	int i,j,k,from,to,next,n,a,b,c,cs=1;//cs 格式控制 :Test Set #1
	while(scanf("%d",&n)!=EOF)//第一行
	{
		memset(dis,0x3f,sizeof(dis));// 数组 先赋值为无穷大
		while(n--)
		{
			scanf("%d",&to);
			dis[1][to] = dis[to][1] = 1; //第一行点to到 1的距离 定为1
		}
		for(i = 2; i <= 19; i++) //第2到19行
		{
			scanf("%d",&a);
			while(a--)//a个点到i的距离 也定为 1
			{
				scanf("%d",&to);
				dis[i][to] = dis[to][i] = 1;
			}
		}
		floyd();
		scanf("%d",&b);//b组 起点、终点
		printf("Test Set #%d\n",cs++);
		while(b--)
		{
			scanf("%d%d",&from, &to);//输入 起点、终点
			printf("%d to %d: %d\n",from,to,dis[from][to]);//格式输出
		}
		printf("\n");//千万别忘了!!!题目输出要求每组测试数据后跟空行
	}
	return 0;
}

版权声明:原创文章,若要转载,请与博主联系,谢谢

时间: 2024-11-08 13:27:51

zoj 1221 Risk【最短路 floyd】的相关文章

zoj 1221

先解释一下题目的意思,看了我好久...;英语不好又不仔细看的人真心伤不起..... 输入19行表示的是 第 i 行就是第 i 个数字相邻的城市的个数的多少,然后输入相邻的城市,只算比 i 大的数,小的就不算了 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1221 解释一下dijkstra 和 flody 区别 dijkstra 是不能有负数边的,因为他每次加入都是目前最小权值的边和对应的 结点,如果有负数边,可能再下一

SDUT 2930-人活着系列之开会(最短路Floyd)

人活着系列之开会 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 人活着如果是为了事业,从打工的到老板的,个个都在拼搏,奋斗了多年终于有了非凡成就,有了一笔丰富的钱财.反过来说,人若赚取了全世界又有什么益处呢?生不带来,死了你还能带去吗?金钱能买保险,但不能买生命,金钱能买药品,但不能买健康,人生在世,还是虚空呀! 在苍茫的大海上,有很多的小岛,每个人都在自己的小岛上.又到了开会的时候了,鹏哥通过飞信告知了每个人,然后大家就开

【bzoj2324】[ZJOI2011]营救皮卡丘 最短路-Floyd+有上下界费用流

原文地址:http://www.cnblogs.com/GXZlegend/p/6832504.html 题目描述 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘,也为了正义,小智和他的朋友们义不容辞的踏上了营救皮卡丘的道路. 火箭队一共有N个据点,据点之间存在M条双向道路.据点分别从1到N标号.小智一行K人从真新镇出发,营救被困在N号据点的皮卡丘.为了方便起见,我们将真新镇视为0号据点,一开始K个人都在0号点. 由于火箭队的重重布防,要想摧毁K号据点,必须

UVa10048_Audiophobia(最短路/floyd)(小白书图论专题)

解题报告 题意: 求所有路中最大分贝最小的路. 思路: 类似floyd算法的思想,u->v可以有另外一点k,通过u->k->v来走,拿u->k和k->v的最大值和u->v比较,存下最小的值. #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define inf 0x3f3f3f3f using namespace std;

POJ训练计划2253_Frogger(最短路/floyd)

解题报告 题意: 求0到1所有路中最大值最小的那个数. 思路: floyd. #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define inf 0x3f3f3f3f using namespace std; int n,m,q; double mmap[210][210]; struct node { double x,y; } p[210]; doub

UVa10099_The Tourist Guide(最短路/floyd)(小白书图论专题)

解题报告 题意: 有一个旅游团现在去出游玩,现在有n个城市,m条路.由于每一条路上面规定了最多能够通过的人数,现在想问这个旅游团人数已知的情况下最少需要运送几趟 思路: 求出发点到终点所有路当中最小值最大的那一条路. 求发可能有多种,最短路的松弛方式改掉是一种,最小生成树的解法也是一种(ps,prime和dijs就是这样子类似的) #include <iostream> #include <cstdio> #include <cstring> #include <

ZOJ 3232 It&#39;s not Floyd Algorithm --强连通分量+Floyd

题意:给你一个传递闭包的矩阵,mp[u][v] = 1表示u可以到达v,为0代表不可到达,问你至少需要多少条边组成的传递闭包符合这个矩阵给出的关系 分析:考虑一个强连通分量,如果这个分量有n个节点,那么至少只需要n条边皆可以满足传递闭包(因为此时形成环就可),所以求出所有的强连通分量,将他们缩成一个个的点,并记录该强连通分量有多少个节点,然后建立新图,在运行一遍floyd算法,去除所有满足 tG[i][k]&&tG[k][j]&&tG[i][j]的边(i,j),然后统计还剩

ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)

这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在于状态转移方程的确立和SPFA的过程 1 //最短路:Floyd+SPFA(BFS)+DP 2 //Time:20Ms Memory:336K 3 //题目很好,数据较弱,网上部分代码有些问题却能够A掉 4 //题意:超级马里奥要从A+B处背着公主以最短路程到达1处,其中1-A是村庄,剩下的是城堡

Problem B Codeforces 295B 最短路(floyd)

Description Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game: The game consists