标签:style http color strong os art
题目链接:
题目:
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
这个题目就是找联通快。。
思路是
1:建图过程,利用并查集找联通快,然后将联通快压缩到一个vector的容器里面。
2:然后对每个联通快进行floyd,floyd的时候重新建图,然后枚举每个起点找最长的一条路,就是烧完联通快的时间,然后加上士兵上船的时间找最小值,然后再各个联通块中找最大的值,即为所求。
代码为:
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<vector> #define INF 0x3f3f3f3f using namespace std; const int maxn=1000+10; vector<int>vec[maxn]; int ori[maxn][maxn],gra[200+10][200+10],root[maxn],vis[maxn],Time[maxn]; int N; int findroot(int x) { if(root[x]!=x) root[x]=findroot(root[x]); return root[x]; } void Merge(int a,int b) { int fx=findroot(a); int fy=findroot(b); if(fx!=fy) root[fx]=fy; } int floyd(int st,int n) { for(int i=0;i<n;i++) for(int j=0;j<n;j++) { int u=vec[st][i]; int v=vec[st][j]; gra[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(gra[i][j]>gra[i][k]+gra[k][j]) gra[i][j]=gra[i][k]+gra[k][j]; } int cal=INF; for(int i=0;i<n;i++) { int tmp=0; for(int j=0;j<n;j++) tmp=max(tmp,gra[i][j]); cal=min(cal,tmp+Time[vec[st][i]]); } return cal; } void read_graph() { int val; memset(ori,INF,sizeof(ori)); for(int i=1;i<=N;i++) root[i]=i; for(int i=1;i<=N;i++) vec[i].clear(); for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) { scanf("%d",&val); if(val!=-1) { ori[i][j]=val; Merge(i,j); } } for(int i=1;i<=N;i++) { int rootx=findroot(i); vec[rootx].push_back(i); } } int main() { while(scanf("%d",&N)!=EOF) { memset(vis,0,sizeof(vis)); read_graph(); int ans=0; for(int i=1;i<=N;i++) scanf("%d",&Time[i]); for(int i=1;i<=N;i++) { int rootx=findroot(i); if(!vis[rootx]) { vis[rootx]=1; ans=max(ans,floyd(rootx,vec[rootx].size())); } } printf("%d\n",ans); } return 0; }
标签:style http color strong os art
原文地址:http://blog.csdn.net/u014303647/article/details/37651273