• SPOJ1825 Free tour II


    SPOJ Problem Set (classical)

    1825. Free tour II

    Problem code: FTOUR2

     

     

    After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, Travel Agent SPOJ goes on with another discount tour.

    The tour will be held on ICPC island, a miraculous one on the Pacific Ocean. We list N places (indexed from 1 to N) where the visitors can have a trip. Each road connecting them has an interest value, and this value can be negative(if there is nothing interesting to view there). Simply, these N places along with the roads connecting them form atree structure. We will choose two places as the departure and destination of the tour.

    Since September is the festival season of local inhabitants, some places are extremely crowded (we call themcrowded places). Therefore, the organizer of the excursion hopes the tour will visit at most K crowded places (too tiring to visit many of them) and of course, the total number of interesting value should be maximum.

    Briefly, you are given a map of N places, an integer K, and M id numbers of crowded place. Please help us to find the optimal tour. Note that we can visit each place only once (or our customers easily feel bored), also the departure and destination places don't need to be different.

    Input

    There is exactly one case. First one line, containing 3 integers N K M, with 1 <= N <= 200000, 0 <= K <= M, 0 <= M<= N.

    Next M lines, each line includes an id number of a crowded place.

    The last (N - 1) lines describe (N - 1) two-way roads connected N places, form a b i, with a, b is the id of 2 places, and i is its interest value (-10000 <= i <= 10000).

    Output

    Only one number, the maximum total interest value we can obtain.

    Example

    Input:
    8 2 3
    3
    5
    7
    1 3 1
    2 3 10
    3 4 -2
    4 5 -1
    5 7 6
    5 6 5
    4 8 3
    
    
    Output:
    12
    ------------------------------------------------------------------------
    题目大意:见漆子超论文。
    解题思路:见漆子超论文。我感觉这个做法有点像钻复杂度的空子,如果不好好计算复杂度什么的,还是不敢这么暴力的做的。。。
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #define clr(a,b) memset(a,b,sizeof(a))
    using namespace std;
    
    const int N=400005,inf=0x7fffffff;
    int n,m,eid,col[N],minn;
    int head[N],ed[N<<1],nxt[N<<1];int val[N<<1],ans,k;
    int vis[N],fa[N],dep[N],siz[N],num[N];int g[N],mg[N];
    
    void addedge(int s,int e,int v){
        ed[eid]=e;val[eid]=v;nxt[eid]=head[s];head[s]=eid++;
    }
    
    bool cmp(int a,int b){
        return dep[ed[a]]<dep[ed[b]];
    }
    
    int dfssize(int s,int f){
        fa[s]=f;siz[s]=1;
        for(int i=head[s];~i;i=nxt[i])
            if(ed[i]!=f&&!vis[ed[i]])siz[s]+=dfssize(ed[i],s);
        return siz[s];
    }
    
    void dfsroot(int s,int sum,int&root){
        int maxx=sum-siz[s];
        for(int i=head[s];~i;i=nxt[i]){
            int e=ed[i];if(e==fa[s]||vis[e])continue;
            dfsroot(e,sum,root);maxx=max(maxx,siz[e]);
        }
        if(maxx<minn){minn=maxx;root=s;}
    }
    
    int dfsdepth(int s,int f){
        dep[s]=col[s];int maxx=0;
        for(int i=head[s];~i;i=nxt[i])
            if(ed[i]!=f&&!vis[ed[i]])maxx=max(maxx,dfsdepth(ed[i],s));
        return dep[s]+=maxx;
    }
    
    void dfsg(int s,int f,int d,int c){
        g[c]=max(g[c],d);
        for(int i=head[s];~i;i=nxt[i]){
            int e=ed[i];int v=val[i];if(e==f||vis[e])continue;
            dfsg(e,s,d+v,c+col[e]);
        }
    }
    
    void solve(int s){
        int sum=dfssize(s,-1),top;minn=inf;
        int root;dfsroot(s,sum,root);vis[root]=1;
        for(int i=head[root];~i;i=nxt[i])
            if(ed[i]!=root&&!vis[ed[i]])solve(ed[i]);top=0;
        for(int i=head[root];~i;i=nxt[i])
            if(ed[i]!=root&&!vis[ed[i]]){dfsdepth(ed[i],root);num[top++]=i;}
        sort(num,num+top,cmp);
        for(int i=0;i<=dep[ed[num[top-1]]];i++)mg[i]=-inf;
        for(int i=0;i<top;i++){
            int e=ed[num[i]],d=dep[e];int v=val[num[i]];
            for(int j=0;j<=d;j++)g[j]=-inf;
            dfsg(e,root,v,col[e]);
            if(i){
                for(int j=0;j<=k-col[root]&&j<=d;j++){
                    int r=min(dep[ed[num[i-1]]],k-col[root]-j);
                    if(mg[r]==-inf)break;
                    if(g[j]!=-inf)ans=max(ans,mg[r]+g[j]);
                }
            }
            for(int j=0;j<=d;j++){
                mg[j]=max(g[j],mg[j]);
                if(j)mg[j]=max(mg[j],mg[j-1]);
                if(j+col[root]<=k)ans=max(ans,mg[j]);
            }
        }
        vis[root]=0;
    }
    
    int main(){
    //    freopen("/home/axorb/in","r",stdin);
    //    freopen("/home/axorb/out","w",stdout);
    //printf("%d %d\n",inf,-inf);
        scanf("%d%d%d",&n,&k,&m);
        eid=0;
        for(int i=1;i<=n;i++){
            col[i]=vis[i]=0;head[i]=-1;
        }
        for(int i=1;i<=m;i++){
            int a;scanf("%d",&a);col[a]=1;
        }
        for(int i=1;i<n;i++){
            int a,b;int c;scanf("%d%d%d",&a,&b,&c);
            addedge(a,b,c);addedge(b,a,c);
        }
        ans=0;solve(1);
        printf("%d\n",ans);
    }
    

      

    也许有挫折,但这些,怎能挡住湘北前进的步伐
  • 相关阅读:
    kendoGrid edit功能
    kendoGrid Event事件
    你不得不看的81条JavaScript编码小技巧,吐血整理,建议收藏
    H265编码视频流媒体播放器EasyPlayer.js支持9宫格视频同屏播放的写法
    EasyNTS 交叉编译海思系统下的可执行程序实现及测试过程
    EasyNTS在Go1.15版本下linux下无法编译的问题优化
    TSINGSEE青犀视频开发Python3行人分析功能运行一段时间后崩溃是什么原因?
    Mysql数据库导入excel和乱码问题
    聊聊5G:5G技术的落地能给视频娱乐、VR直播带来怎样的潜力?
    企业直播的核心点在哪里?未来将如何发展?
  • 原文地址:https://www.cnblogs.com/Fatedayt/p/2581276.html
Copyright © 2020-2023  润新知