并查集维护联通性
每一个联通快枚举点燃的船只,然后求出全部烧着的最小时间
最后求出所有联通块烧着时间的最大值即可。
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
8
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<iterator>
using namespace std;
#define MAXN 1100
#define INF 0x333f3f3f
vector<int> sets[MAXN];
int g[200][200],ori[MAXN][MAXN];
int dist[MAXN];
int n,set[MAXN],vis[MAXN];
int floyd(int no,int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int u=sets[no][i];
int v=sets[no][j];
g[i][j]=ori[u][v];
}
}
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(g[i][k]+g[k][j]<g[i][j])
g[i][j]=g[i][k]+g[k][j];
}
int dis=0x3f3f3f3f;
for(int i=0;i<n;i++)
{
int tmp=0;
for(int j=0;j<n;j++)
tmp=max(tmp,g[i][j]);
dis=min(dis,tmp+dist[sets[no][i]]);
}
return dis;
}
int find(int a)
{
int root=a,temp;
while(set[root]!=root) root=set[root];
while(set[a]!=root)
{
temp=a;
a=set[a];
set[temp]=root;
}
return root;
}
bool merge(int a,int b)
{
int x=find(a);
int y=find(b);
if(x==y) return false;
set[x]=y;
return true;
}
int main()
{
while(~scanf("%d",&n))
{
int dis;
for(int i=0;i<=n;i++) set[i]=i;
for(int u=1;u<=n;u++)
{
for(int v=1;v<=n;v++)
{
scanf("%d",&dis);
ori[u][v]=dis==-1?INF:dis;
if(dis!=-1) merge(v,u);
}
}
for(int i=1;i<=n;i++) scanf("%d",&dist[i]);
for(int i=1;i<=n;i++) sets[i].clear();
for(int i=1;i<=n;i++)
{
int root=find(i);
sets[root].push_back(i);
}
int ans=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
int root=find(i);
if(vis[root]) continue;
vis[root]=1;
ans=max(ans,floyd(root,sets[root].size()));
}
cout<<ans<<endl;
}
return 0;
}