Jarvis's Blog


  • Home

  • Tags

  • Categories

  • Archives

  • others

PAT 1003. Battle Over Cities

Posted on 2016-07-30 | In Algorithm

题目描述

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.

Input

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

Output

For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.
Sample Input
3 2 3
1 2
1 3
1 2 3

Sample Output
1
0
0

算法分析

这道题目用到的是Union Find, 即并查集。

并查集是一种树型的数据结构,用于处理一些不相交集合的合并及查询问题。常常在使用中以森林来表示。集就是让每个元素构成一个单元素的集合,也就是按一定顺序将属于同一组的元素所在的集合合并。

Read more »

PAT 1003. Emergency

Posted on 2016-07-30 | In Algorithm

题目描述

Description
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Sample Output
2 4

算法分析

用dfs解决,求出最短路径
但注意题目需要输出的是

the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather

Read more »

初学者的贪吃蛇游戏

Posted on 2016-01-27 | In Others

C语言贪吃蛇游戏制作

初学编程,利用现有的知识编写了一个贪吃蛇小游戏。
目前游戏的制作程度仅限于“能玩”。
还有一定BUG有待改善。

Read more »
1…56
Jarvis Wong

Jarvis Wong

Ideas worth spreading.

43 posts
9 categories
30 tags
Ace-0 Gmail w674--7 Twitter
© 2018 — 2019 Zhijun Wang. All rights reserved.
Powered by Hexo
|
Theme — NexT.Gemini v6.0.3