watashi's mm is so pretty as well as smart. Recently, she has watched the movie Chibi. So she knows more about the War of ChiBi. In the war, Cao 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 1m/s. You also know the time your soldiers need to travel from your camp to each boat. Because burning Cao 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 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 next N lines, each line contains N integers 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). Then N intergers 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 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 Cao's boats counting from the soldiers leave the camp in a single line.
Sample input
4 0 1 2 -1 1 0 4 -1 2 4 0 -1 -1 -1 -1 0 1 2 4 8
Sample Output
8Author: CHAO, Jiansong
Source: ZOJ Monthly, December 2008
关键是求出每个连通分量的“半径”,由于题目保证每个连通分量大小不超过100,所以可以先用floyd求出连通分量内任意两点间的距离,再求答案即可。
#include<cstdio> #include<map> #include<queue> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<list> #include<set> #include<cmath> using namespace std; const int maxn = 1e3 + 5; const int INF = 1e9; const double eps = 1e-6; typedef unsigned long long ULL; typedef long long LL; typedef pair<int, int> P; #define fi first #define se second int dis[maxn][maxn]; vector<int> G[maxn]; int t[maxn]; int vis[maxn]; int n, cnt; void dfs(int x){ G[cnt].push_back(x); for(int i = 0;i < n;i++){ if(!vis[i] && dis[x][i] != -1){ vis[i] = 1; dfs(i); } } } int dp[maxn][maxn]; void floyd(int id){ int Size = G[id].size(); for(int i = 0;i < Size;i++){ for(int j = 0;j < Size;j++){ int x = G[id][i]; int y = G[id][j]; dp[i][j] = dis[x][y]; if(dp[i][j] == -1) dp[i][j] = INF; } } for(int k = 0;k < Size;k++){ for(int i = 0;i < Size;i++){ for(int j = 0;j < Size;j++){ dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]); } } } } int main(){ while(cin >> n){ for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++) cin >> dis[i][j]; } for(int i = 0;i < n;i++){ cin >> t[i]; } for(int i = 0;i < n;i++){ G[i].clear(); } memset(vis, 0, sizeof vis); cnt = 0; for(int i = 0;i < n;i++){ if(!vis[i]){ vis[i] = 1; dfs(i); cnt++; } } int ans = 0; for(int i = 0;i < cnt;i++){ floyd(i); int Min = INF; for(int j = 0;j < G[i].size();j++){ int x = G[i][j]; int Max = 0; for(int k = 0;k < G[i].size();k++){ Max = max(Max, dp[j][k]); } Min = min(Min, t[x]+Max); } ans = max(ans, Min); } cout << ans << endl; } return 0; }