标签:style blog color os strong io 数据 for
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表示不联通), 权值表示距离,现在要你派若干人去放火,已知火势蔓延速度为1m/s,兵营到每艘船的时间已知,要求在派出最少人的情况下求出战船全部燃烧的最短时间。
分析:
首先这是个无向非联通图,派出的人最少也就是说每个联通块派一个人;然后我们需要枚举每一艘船作为起点,用SPFA(起点船到所有船的最短路径)选择最大的值表明最大的最短路径肯定经过所有点,所有联通块最大值中的最小值就是该联通块全部点燃的最短时间,然后取这些联通块最短时间的最大值就是所求全部燃烧的最短时间。
#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++){//枚举联通块中所有的点 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<hCount;i++){//每个联通块 mi=INT_MAX; for(j=0;j<Count[i];j++){//联通块中的所有点 联通块的索引 mi=min(mi,startDis[grap[i][j]]+spfa(j,Count[i],grap[i])); } ans=max(ans,mi); } printf("%d\n",ans); } int main(void) { int i,j; while(scanf("%d",&n)!=EOF) { pre();//接收数据 init();//获得联通块数组 solve();//遍历联通块中所有点 } return 0; }
ZOJ 3080 ChiBi(spfa),布布扣,bubuko.com
标签:style blog color os strong io 数据 for
原文地址:http://www.cnblogs.com/woshijishu3/p/3888645.html