Zoj problem set-3080 Chibi Time Limit: 5 seconds memory limit: 32768 KB
Watashi's mm is so pretty as well as smart. Recently, she has watched the movieChibi. So she knows more about the war of Chibi. in the war, Cao had 800,000 soldiers, much more than his opponents '. but he was defeated. one of the mistakes he made was that he connected some of his boats together, and these boats were burned by the clever opponents.
Then an interesting problem occurs to watashi's mm. she wants to use this problem to check whether watashi is as smart as her. however, watashi has no idea about the problem. so he turns to you for help.
You know whether two boats are directly connected and the distance between them. and fire's speed to spread between boats is 1 m/s. you also know the time your soldiers need to travel from your camp to each boat. because burning Cao's boat is a very dangerous job, you must choose the least number of soldiers, and each one can only burn one boat. how much time do you need to burn all the Cao' S boats?
Input
The input contains several test cases. Each test case begins with a line contains only one integer 0 <=N<= 1000, which indicates the number of boats. The nextN lines, Each line containsNIntegers in range [0, 10000], the jth number in the ith line is the distance in metre between the ith boat and the jth boat, if the number is-1, then these two boats are not directly connected (d (I, j) = D (J, I) & D (I, I) = 0 ). thenNIntergers in range [0, 10000], the ith number is the time in second your soldiers need to travel from the camp to the ith boat. what's more Cao is not that stupid, so he won't connect more than 100 boats together.
Output
The shortest time you need to burn all the Cao's boats counting from the soldiers leave the camp in a single line.
Sample Input
40 1 2 -11 0 4 -12 4 0 -1-1 -1 -1 01 2 4 8
Sample output
8
Question:
The matrix shows the connectivity between the CAO boss and the ship (-1 indicates that the ship is not connected). The weight indicates the distance. Now you need to send several people to set fire. It is known that the fire spread at 1 Mb/s, the time required for the barracks to reach each ship is known, and the minimum time required to burn all the warships should be obtained when the minimum number of people is dispatched.
Analysis:
First of all, this is an undirected non-Unicom diagram. The dispatched personnel are at least one person for each Unicom block. Then we need to enumerate every ship as the starting point, use spfa (the shortest path from the starting ship to all ships) to select the maximum value, which indicates that the maximum shortest path must pass through all points, the minimum value of the maximum value of all connected blocks is the shortest time for all connected blocks to be ignited. the maximum value of the shortest time of these connected blocks is the shortest time for burning all the connected blocks.
# Include <queue> # include <stdio. h> # include <limits. h> # include <string. h ># include <iostream> using namespace STD; # define Max 1100 # define INF 10001 # define min (x, y) (x) <(y )? (X) :( y) # define max (x, y) (x)> (y )? (X) :( y) int map [Max] [Max], startdis [Max], n; int grap [Max] [Max], Father [Max], count [Max], hcount; int visit [Max]; void pre () {int I, j; for (I = 0; I <n; I ++) {for (j = 0; j <n; j ++) {scanf ("% d", & map [I] [J]); if (Map [I] [J] =-1) map [I] [J] = inf ;}} for (I = 0; I <n; I ++) scanf ("% d", & startdis [I]);} void DFS (INT block [Max], Int & count, int v) {int I; block [count ++] = V; for (I = 0; I <n; I ++) {if (I! = V & map [v] [I] <INF & visit [I] = 0) {visit [I] = 1; father [I] = V; DFS (Block, Count, I) ;}} void Init () {int I; memset (count, 0, sizeof (count); for (I = 0; I <n; I ++) Father [I] = I; hcount = 0; for (I = 0; I <n; I ++) {If (father [I] = I) {memset (visit, 0, sizeof (visit); visit [I] = 1; DFS (GRAP [hcount], count [hcount], I); hcount ++ ;}} int spfa (int v, int count, int num [Max]) {int I, J; int dis [Max], times [Max], inqueue [Max]; queue <int> q; For (I = 0; I <count; I ++) dis [I] = int_max/10; memset (times, 0, sizeof (times); memset (inqueue, 0, sizeof (inqueue); Times [v] = 1, inqueue [v] = 1, Q. push (V); DIS [v] = 0; while (! Q. empty () {int x = Q. front (); q. pop (), inqueue [x] = 0; for (I = 0; I <count; I ++) {// enumerate all vertices in the UNICOM block if (Num [v]! = Num [I] & map [num [x] [num [I] <INF & dis [I]> dis [x] + map [num [x]] [num [I]) {dis [I] = dis [x] + map [num [x] [num [I]; If (inqueue [I] = 0) {q. push (I); inqueue [I] = 1; Times [I] ++; If (Times [I]> = count) Return-1 ;}}}} int Mm = 0; For (INT I = 0; I <count; I ++) Mm = max (DIS [I], mm); Return mm;} void solve () {int I, j, ANS = 0, mi; for (I = 0; I