POJ 3160 Father Christmas flymouse

37 篇文章 0 订阅

Father Christmas flymouse

Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 3484 Accepted: 1188
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

POJ Monthly–2006.12.31, Sempr

题目大意:flymouse要去给人送礼,每个人分布在不同的地方,且他们之间有m条单向边,另外每个人获得礼物后flymouse会得到一个舒心值。现flymouse从某个人开始,沿单向边访问下一个人(其中点可以访问多次,但舒心值只能获得一次),求他能获得的最大舒心值。

缩点、建新图、每个入读为零的点来一次SPFA

#include<iostream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<queue>
using namespace std;
const int MAXN = 30005;
const int MAXM = 150005;
struct Edge{ int to,next; }e[MAXM];
int dfn[MAXN],low[MAXN],head[MAXN],link[MAXN],visx,tot,tot_edge;
int n,m,cnt,belong[MAXN],in[MAXN],dis[MAXN],a[MAXN],sum[MAXN];
bool exist[MAXN],vis[MAXN];
void PrePare(){
    cnt=0;visx=0;tot=0;
    memset(dfn,-1,sizeof dfn );memset(low,-1,sizeof low );
    memset(head,0,sizeof head );memset(link,0,sizeof link );
    memset(belong,0,sizeof belong );memset(exist,0,sizeof exist );
    memset(sum,0,sizeof sum );memset(in,0,sizeof in );
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),a[i]=max(a[i],0);
}
void Add_Edge(int u,int v,int *head){
    e[++tot].to=v;e[tot].next=head[u];head[u]=tot;
}
stack<int> st;
void Tarjan(int u){
    dfn[u]=low[u]= ++visx;
    st.push(u);exist[u]=true;
    for(int i=head[u];i;i=e[i].next){
        int v=e[i].to;
        if(dfn[v]==-1){
            Tarjan(v);
            low[u]=min(low[v],low[u]);
        }
        else if(exist[v]) low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u]){
        ++cnt;//寮鸿繛閫氬垎閲忎釜鏁?
        while(1){
            int v=st.top();st.pop();
            exist[v]=0;belong[v]=cnt;
            sum[cnt]+=a[v];
            if(v==u) break;
        }
    }
}
void Make(){
    for(int i=1;i<=n;i++)
        for(int j=head[i];j;j=e[j].next){
            int v=e[j].to;
            if(belong[i]!=belong[v]){
                Add_Edge(belong[i],belong[v],link);
                in[belong[v]]++;
            }
        }
}
queue<int> q;
int SPFA(int S){
    int rt=dis[S]=sum[S];
    memset(vis,0,sizeof vis );
    q.push(S);vis[S]=1;
    while(!q.empty()){
        int u=q.front();q.pop();vis[u]=false;
        for(int i=link[u];i;i=e[i].next){
            int v=e[i].to;
            dis[v]=max(dis[v],dis[u]+sum[v]);
            rt=max(rt,dis[v]);
            if(!vis[v]) q.push(v),vis[v]=1;
        }
    }
    return rt;

}
int main(){
    while(~scanf("%d%d",&n,&m)){
        PrePare();
        for(int u,v,i=1;i<=m;i++){
            scanf("%d%d",&u,&v);
            Add_Edge(u+1,v+1,head);
        }
        memset(dis,0,sizeof dis );
        for(int i=1;i<=n;i++) if(dfn[i]==-1) Tarjan(i);
        Make();
        int ans=0;
        for(int i=1;i<=cnt;i++)
            if(in[i]==0)
                ans=max(ans,SPFA(i));
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值