zoukankan      html  css  js  c++  java
  • POJ 3160 Father Christmas flymouse (tarjan+spfa)

    Father Christmas flymouse
    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 1999   Accepted: 674

    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 andM 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 jindicating 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

     
     
    题意:圣诞节,flymouse 要给学生送礼物,他从任意一个房间出发,每打开门送出礼物后,每个学生有一个值(可正可负), 他可以多次路过不进,但进的话只能进一次。flymouse走到头,求他能得到的最大值

    思路:因为有环,所以进行缩点 构建新图 但新的图可能有多个入度出度为0的点,如果用枚举的话 时间可能会超 可以建立一个超级源点,把所有入度为0的点连起来,然后求src到 出度为0的点中值最大的就OK
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int VM=30010;
    const int EM=150010;
    
    int val[VM],newval[VM],head[VM],head1[VM],cnt,n;    //val[]是原点的值,newval是缩点的点的值
    int vis[VM],belong[VM],dfn[VM],low[VM],stack[VM];   //这是tarjan的常用数组变量
    int indeg[VM],outdeg[VM];
    int scc,top,k,src=0;    //src 为超级源点
    
    struct Edge{
        int to,nxt;
    }edge[EM];  //旧图
    
    struct Edge1{
        int to,cap;
        int nxt;
    }edge1[EM]; //新图
    
    void addedge(int cu,int cv){
        edge[cnt].to=cv;
        edge[cnt].nxt=head[cu];
        head[cu]=cnt++;
    }
    
    void addedge1(int cu,int cv,int cw){
        edge1[cnt].to=cv;
        edge1[cnt].cap=cw;
        edge1[cnt].nxt=head1[cu];
        head1[cu]=cnt++;
    }
    
    void Tarjan(int u){  //tarjan算法缩点
        int v;
        dfn[u]=low[u]=++k;
        stack[top++]=u;
        vis[u]=1;
        for(int i=head[u];i!=-1;i=edge[i].nxt){
            v=edge[i].to;
            if(!dfn[v]){
                Tarjan(v);
                low[u]=low[u]<low[v]?low[u]:low[v];
            }else if(vis[v] && low[u]>dfn[v])
                low[u]=dfn[v];
        }
        if(dfn[u]==low[u]){
            scc++;
            do{
                v=stack[--top];
                vis[v]=0;
                belong[v]=scc;
            }while(u!=v);
        }
    }
    
    void Solve(){
        scc=top=k=0;
        memset(vis,0,sizeof(vis));
        memset(dfn,0,sizeof(dfn));
        for(int u=1;u<=n;u++)
            if(!dfn[u])
                Tarjan(u);
    }
    
    void new_graph(){
        int u,v;
        memset(newval,0,sizeof(newval));
        memset(indeg,0,sizeof(indeg));
        memset(outdeg,0,sizeof(outdeg));
        for(u=1;u<=n;u++)    //每个强连通中的正值加起是最大的
            if(val[u]>0)
                newval[belong[u]]+=val[u];
        cnt=0;
        for(u=1;u<=n;u++)   //建立新图,和求出入度出度
            for(int i=head[u];i!=-1;i=edge[i].nxt){
                v=edge[i].to;
                if(belong[u]!=belong[v]){
                    indeg[belong[v]]++;
                    outdeg[belong[u]]++;
                    addedge1(belong[u],belong[v],newval[belong[v]]);
                }
            }
        for(u=1;u<=scc;u++) //超级源点与入度为0的点连起
            if(!indeg[u])
                addedge1(src,u,newval[u]);
    }
    
    int SPFA(){
        int dis[VM],s[VM],vs[VM];
        top=0;
        memset(dis,0,sizeof(dis));
        memset(vs,0,sizeof(vs));
        s[top++]=src;
        vs[src]=1;
        while(top){
            int u=s[--top];
            vs[u]=0;
            for(int i=head1[u];i!=-1;i=edge1[i].nxt){
                int v=edge1[i].to;
                if(dis[v]<dis[u]+edge1[i].cap){
                    dis[v]=dis[u]+edge1[i].cap;
                    if(!vs[v]){
                        vs[v]=1;
                        s[top++]=v;
                    }
                }
            }
        }
        int MAX=0;  
        for(int i=1;i<=scc;i++) //取到出度为0中最大的
            if(outdeg[i]==0)
                MAX=max(MAX,dis[i]);
        return MAX;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int m;
        while(~scanf("%d%d",&n,&m)){
            memset(head1,-1,sizeof(head1));
            memset(head,-1,sizeof(head));
            cnt=0;
            for(int i=1;i<=n;i++)
                scanf("%d",&val[i]);
            int u,v;
            while(m--){
                scanf("%d%d",&u,&v);
                addedge(u+1,v+1);   //个人习惯 从1开始而已
            }
            Solve();
            new_graph();
            int ans=SPFA();
            printf("%d\n",ans);
        }
        return 0;
    }
  • 相关阅读:
    异常单据锁定涉及的数据库表
    用友通只启用核算模块
    一个迟到MM如何让老师疯掉的
    偷偷看,别笑出声啊
    安装时又提示删除程序
    自动折行的设置
    酒后百态新编 恶搞
    系统管理中“站点”“运行状态”的含义
    神经病女士银行取钱
    HDOJ 1017
  • 原文地址:https://www.cnblogs.com/jackge/p/3053976.html
Copyright © 2011-2022 走看看