ZOJ 3080 ChiBi(SPFA,并查集)

6 篇文章 0 订阅
2 篇文章 0 订阅

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3080

ChiBi

Time Limit: 5 Seconds       Memory Limit: 32768 KB

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求出联通块内到起点的最大值,所有最大值中的最小值就是该联通块全部点燃的最短时间,然后取这些联通块最短时间的最大值就是所求全部燃烧的最短时间。

注意这里有两艘不同的船距离为0的点,被这个数据坑了3个小时。


#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define mm 1000007
#define nn 1007

using namespace std;

int u[mm],v[mm],w[mm],nex[mm],fir[nn];
int maxe;
int d[nn],q[nn];
bool inq[nn];
int f,r;
int n,m;
int def[nn];//记录兵营到每艘船的时间
int pre[nn];
int rec[nn];//记录每个联通块点燃的最短时间

inline int ReadInt()
{
    int flag=0;
    char ch = getchar();
    int data = 0;
    while (ch < '0' || ch > '9')
    {
        if(ch=='-') flag=1;
        ch = getchar();
    }
    do
    {
        data = data*10 + ch-'0';
        ch = getchar();
    }while (ch >= '0' && ch <= '9');
        if(flag) data=-data;
        return data;
}

int root(int x)
{
    if (pre[x]==x)  return x;
    return pre[x]=root(pre[x]);
}

inline void intree(int x,int y)
{
    pre[root(x)]=pre[root(y)];
}

inline void read_graph()
{
    int _w;
    maxe=-1;
    memset(fir,-1,(n+1)*sizeof (int));
    for (int i=1;i<=n;i++)
    {
        for (itn j=1;j<=n;j++)
        {
            _w=ReadInt();
            if (_w==-1 || _w==0 && i==j) continue;//坑点,有不同的船距离为0

            intree(i,j);

            maxe++;
            u[maxe]=i;
            v[maxe]=j;
            w[maxe]=_w;
            nex[maxe]=fir[i];
            fir[i]=maxe;
        }
    }
    for (int i=1;i<=n;i++)
    {
        def[i]=ReadInt();
    }
}

inline int SPFA(int _u)
{
    itn MAX=0;
    memset(inq,0,(n+1)*sizeof(int));
    f=0;r=-1;
    memset(d,0x3f,(n+1)*sizeof(int));
    d[_u]=0;
    q[++r]=_u;
    inq[_u]=true;
    while (f<=r)
    {
        int x=q[f++];
        inq[x]=false;
        for (int e=fir[x];e!=-1;e=nex[e])
        {
            if (d[v[e]]>d[x]+w[e])
            {
                d[v[e]]=d[x]+w[e];

                if (!inq[v[e]])
                {
                    inq[v[e]]=true;
                    q[++r]=v[e];
                }
            }
        }
    }
    for (int i=1;i<=n;i++)
        if (d[i]!=INF)
            MAX=max(MAX,d[i]);
    return MAX;
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("/home/fcbruce/文档/code/t","r",stdin);
    #endif // ONLINE_JUDGE

    while (~scanf("%d",&n))
    {
        for (int i=1;i<=n;i++)
            pre[i]=i;
        read_graph();

        int MAX=0;
        memset(rec,0x3f,(n+1)*sizeof(int));
        for (int i=1;i<=n;i++)
        {
            int k=root(i);//一个联通块内的根节点相同
            rec[k]=min(rec[k],SPFA(i)+def[i]);
        }

        for (itn i=1;i<=n;i++)
        {
            MAX=max(MAX,rec[root(i)]);
        }
        printf("%d\n",MAX);

    }


    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值