zoukankan      html  css  js  c++  java
  • Father Christmas flymouse

    Father Christmas flymouse
    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 3479   Accepted: 1185

    Description

    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.

    Source

    【思路】

    最大点权值路径 tarjian缩点+spfa

    缩点的权值只记录正的值。

    【code】

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<algorithm>
    #include<queue>
    using namespace std;
    #define ME  150005
    #define NM  500009
    vector<int>vec[NM];
    int n,m,val[NM],head[NM],dfn[NM],low[NM],u,v;
    int col[NM],belong[NM],sumcol,ans,in[NM],out[NM];
    int sumedge,stack[NM],instack[NM],dis[NM],inq[NM];
    int tim,top;
    struct Edge {
        int x,y,nxt;
        Edge(int x=0,int y=0,int nxt=0):
            x(x),y(y),nxt(nxt) {}
    } edge[ME];
    void add(int x,int y)
    {
        edge[++sumedge]=Edge(x,y,head[x]);
        head[x]=sumedge;
    }
    void fir()
    {
        memset(head,0,sizeof(head));
        memset(stack,0,sizeof(stack));
        memset(instack,0,sizeof(instack));
        memset(dis,0,sizeof(dis));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(out,0,sizeof(out));
        memset(in,0,sizeof(in));
        memset(inq,0,sizeof(inq));
        for(int i=0; i<n; i++)
            vec[i].clear();
        top=0;
        sumedge=0;
        sumcol=0;
        tim=0;
    }
    void tarjian(int x)
    {
        dfn[x]=low[x]=++tim;
        stack[top++]=x;
        instack[x]=1;
        for(int i=head[x]; i; i=edge[i].nxt)
            if(instack[edge[i].y])
                low[x]=min(low[x],dfn[edge[i].y]);
            else if(!dfn[edge[i].y]) {
                tarjian(edge[i].y);
                low[x]=min(low[x],low[edge[i].y]);
            } else {
            }
        if(low[x]==dfn[x]) {
            sumcol++;
            while(stack[top-1]!=x) {
                col[stack[top-1]]=sumcol;
                instack[stack[top-1]]=false;
                top--;
            }
            stack[top]=sumcol;
            top--;
        }
    }
    void spfa()
    {
        queue<int>q;
        q.push(0);
        inq[0]=1;
        while(!q.empty()) {
            int now=q.front();
            q.pop();
            inq[now]=0;
            for(int i=vec[now].size()-1; i>=0; i--) {
                int to=vec[now][i];
                if(dis[to]<dis[now]+belong[to]) {
                    dis[to]=dis[now]+belong[to];
                    if(!inq[to]) {
                        inq[to]=1;
                        q.push(to);
                    }
    
                }
            }
        }
    }
    int main()
    {
        while(scanf("%d %d",&n,&m)) {
            fir();
            for(int i=0; i<n; i++)
            {
                scanf("%d",&val[i]);
                val[i]=max(val[i],0);
            }
            
            for(int i=1; i<=m; i++)
            {
                scanf("%d %d",&u,&v);
                add(u,v);
            }
            for(int i=0; i<n; i++)
                if(!dfn[i])tarjian(i);
                for(int i=0;i<n;i++)cout<<col[i]<<endl;
            for(int i=0; i<n; i++)
                belong[col[i]]+=val[i];
            for(int i=0; i<n; i++) {
                for(int j=head[i]; j; j=edge[j].nxt) {
                    if(col[i]==col[edge[j].y])continue;
                    vec[col[i]].push_back(col[edge[i].y]);
                    out[col[i]]++;
                    in[col[edge[i].y]]++;
                }
            }
            for(int i=1;i<=sumcol;i++)
            if(!in[i])vec[0].push_back(i);
            spfa();
            for(int i=1; i<=sumcol; i++) {
                if(!out[i])ans=max(ans,dis[i]);
            }
            printf("%d
    ",ans);
        }
           return 0;
    }
  • 相关阅读:
    robot framework 文本校验,文本与关键字重复的处理
    python pip切本地源
    sqlalchemy 多个数据库地址配置
    python SQLAlchemy的简单配置和查询
    根据列表中字典中的某个value排序
    python SQLAlchemy中子查询subquery的使用
    python SQLAlchemy中query与query()
    weekly paper read
    C++调用windowsAPI
    English 邮件
  • 原文地址:https://www.cnblogs.com/zzyh/p/6959230.html
Copyright © 2011-2022 走看看