zoukankan      html  css  js  c++  java
  • poj3160强连通分量加dfs

    After retirement as contestant from WHU ACM Team, flymouse volunteered to do the odds and ends such as cleaning out the computer lab for training as extension of his contribution to the team. When Christmas came, flymouse played Father Christmas to give gifts to the team members. The team members lived in distinct rooms in different buildings on the campus. To save vigor, flymouse decided to choose only one of those rooms as the place to start his journey and follow directed paths to visit one room after another and give out gifts en passant until he could reach no more unvisited rooms.

    During the days on the team, flymouse left different impressions on his teammates at the time. Some of them, like LiZhiXu, with whom flymouse shared a lot of candies, would surely sing flymouse’s deeds of generosity, while the others, like snoopy, would never let flymouse off for his idleness. flymouse was able to use some kind of comfort index to quantitize whether better or worse he would feel after hearing the words from the gift recipients (positive for better and negative for worse). When arriving at a room, he chould choose to enter and give out a gift and hear the words from the recipient, or bypass the room in silence. He could arrive at a room more than once but never enter it a second time. He wanted to maximize the the sum of comfort indices accumulated along his journey.

    Input

    The input contains several test cases. Each test cases start with two integers N and M not exceeding 30 000 and 150 000 respectively on the first line, meaning that there were N team members living in N distinct rooms and M direct paths. On the next N lines there are N integers, one on each line, the i-th of which gives the comfort index of the words of the team member in the i-th room. Then follow M lines, each containing two integers i and j indicating a directed path from the i-th room to the j-th one. Process to end of file.

    Output

    For each test case, output one line with only the maximized sum of accumulated comfort indices.

    Sample Input

    2 2
    14
    21
    0 1
    1 0

    Sample Output

    35

    Hint

    32-bit signed integer type is capable of doing all arithmetic.

    #include<map>
    #include<set>
    #include<list>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 1000000007
    
    using namespace std;
    
    const int N=30005,maxn=100005,inf=0x3f3f3f3f;
    
    int n,m,index,num,cnt;
    int dfn[N],low[N],value[N],a[N];
    int in[N],out[N];
    int ins[N],inans[N];
    vector<int>v[N],kk[N],ans[N];
    stack<int>s;
    
    void tarjan(int u)
    {
        ins[u]=2;
        dfn[u]=low[u]=++index;
        s.push(u);
        for(int i=0;i<v[u].size();i++)
        {
            int t=v[u][i];
            if(!dfn[t])
            {
                tarjan(t);
                low[u]=min(low[u],low[t]);
            }
            else if(ins[t]==2)low[u]=min(low[u],dfn[t]);
        }
        if(dfn[u]==low[u])
        {
            ++num;
            while(!s.empty()){
                int k=s.top();
                s.pop();
                ins[k]=1;
                inans[k]=num;
                a[num]+=value[k];
                ans[num].push_back(k);
                if(k==u)break;
            }
        }
    }
    int dfs(int u)
    {
        if(!dfn[u])
        {
            int res=0;
            dfn[u]=1;
            for(int i=0;i<kk[u].size();i++)
                res=max(res,dfs(kk[u][i]));
            a[u]+=res;
        }
        return a[u];
    }
    int main()
    {
        while(cin>>n>>m){
            memset(dfn,0,sizeof(dfn));
            memset(low,0,sizeof(low));
            memset(ins,0,sizeof(ins));
            memset(inans,0,sizeof(inans));
            memset(in,0,sizeof(in));
            memset(out,0,sizeof(out));
            memset(a,0,sizeof(a));
            for(int i=0;i<n;i++)
            {
                v[i].clear();
                ans[i].clear();
                kk[i].clear();
            }
            while(!s.empty())s.pop();
            for(int i=0;i<n;i++)
            {
                cin>>value[i];
                if(value[i]<0)value[i]=0;
            }
            while(m--){
                int a,b;
                cin>>a>>b;
                v[a].push_back(b);
            }
            for(int i=0;i<n;i++)
                if(!dfn[i])
                   tarjan(i);
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<v[i].size();j++)
                {
                    int p=v[i][j];
                    if(inans[p]!=inans[i])
                        kk[inans[i]].push_back(inans[p]);
                }
            }
        /*    for(int i=1;i<=num;i++)
            {
                for(int j=0;j<kk[i].size();j++)
                    cout<<kk[i][j]<<" ";
                cout<<endl;
            }*/
            int res=0;
            memset(dfn,0,sizeof(dfn));
            for(int i=1;i<=num;i++)
            {
                res=max(res,dfs(i));
            }
            cout<<res<<endl;
        }
        return 0;
    }

    此题也可以用spfa做,但是我感觉dfs更简便,只是时间复杂度有点高,spfa只需遍历一遍

    先缩点然后dfs找最大的就行了

  • 相关阅读:
    android 防止按钮连续点击的方法(Button,ImageButton等)
    android listview 异步加载图片并防止错位
    Smart3D系列教程6之 《案例实战演练3——倾斜数据正射影像及DSM的生产》
    Smart3D系列教程5之 《案例实战演练2——大区域的地形三维重建》
    Smart3D系列教程4之 《案例实战演练1——小物件的照片三维重建》
    Smart3D系列教程3之 《论照片三维重建中Smart3D几个工作模块的功能意义》
    Smart3D系列教程2之 《为什么三维重建效果这么差?——探探那些被忽略的拍照要求和技巧》
    Smart3D系列教程1之《浅谈无人机倾斜摄影建模的原理与方法》
    如何通过倾斜摄影数据手动配置s3c索引文件?
    无人机倾斜摄影三维展示应该如此简单
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6673221.html
Copyright © 2011-2022 走看看