ZOJ3080 ChiBi(SPFA)

来源:互联网 发布:淘宝衣服直播主播招聘 编辑:程序博客网 时间:2024/06/03 13:36
K - ChiBi
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Submit Status

Description

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

40 1 2 -11 0 4 -12 4 0 -1-1 -1 -1 01 2 4 8

Sample Output

8

题意:求在最少士兵条件下,烧光曹船的最小时间。分析:无向非连通图。用并差集做联通块,并让一个联通块所有点指向同一父节点,然后枚举所有点并将结果更新在其ans[fa[i]](上一操作使同一联通块fa值相同).最后统计所用联通块最大值即可
#include<iostream>#include<cstring>#include<cmath>#include<queue>#include<cstdio>using namespace std;const int INF=0x3f3f3f3f;const int MAXN=1000+6;const int MAXM=MAXN*MAXN;int u[MAXM],v[MAXM],w[MAXM];int ans[MAXN],first[MAXN],inq[MAXN];int next[MAXM];int leng[MAXN],dis[MAXN],fa[MAXN];int n,cnt;int _find(int x){    while(x!=fa[x])        x=fa[x];    return x;}void presolve(){int i;for(i=0;i<n;i++)fa[i]=i;    for(i=0;i<cnt;i++)    {        int x=_find(u[i]);        int y=_find(v[i]);        if(x!=y)fa[x]=y;    }    for(i=0;i<n;i++)        fa[i]=_find(i);}void add_d(int a,int b,int c){    u[cnt]=a;    v[cnt]=b;    w[cnt]=c;    next[cnt]=first[a];    first[a]=cnt++;}int solve(int x){    queue<int>q;    memset(dis,0x3f,sizeof dis);    memset(inq,0,sizeof inq);    q.push(x);dis[x]=0;    inq[x]=1;    while(!q.empty())    {        int e,x=q.front();q.pop();        inq[x]=0;        for(e=first[x];e!=-1;e=next[e])            if(dis[u[e]]+w[e]<dis[v[e]])            {                dis[v[e]]=dis[u[e]]+w[e];                if(!inq[v[e]])                {                    q.push(v[e]);                    inq[v[e]]=1;                }            }    }    int i,sum=0;    for(i=0;i<n;i++)        if(dis[i]>sum&&dis[i]!=INF)sum=dis[i];    return leng[x]+sum;}int main(){    int i,j,k,x;//freopen("123.txt","r",stdin);    while(~scanf("%d",&n))    {        cnt=0;memset(first,-1,sizeof first);memset(next,-1,sizeof next);        for(i=0;i<n;i++)            for(j=0;j<n;j++)            {                scanf("%d",&x);                if(i!=j&&x!=-1)add_d(i,j,x);            }        for(i=0;i<n;i++)            scanf("%d",&leng[i]);        presolve();        memset(ans,0x3f,sizeof ans);        int key;        for(i=0;i<n;i++)            if((key=solve(i))<ans[fa[i]])ans[fa[i]]=key;        int sum=0;        for(i=0;i<n;i++)            if(ans[i]!=INF&&ans[i]>sum)sum=ans[i];        printf("%d\n",sum);    }    return 0;}


0 0