zoj3527
来源:互联网 发布:linux基础命令 编辑:程序博客网 时间:2024/04/29 15:10
2300 years ago, Moriya Suwako was defeated by Yasaka Kanako in the Great Suwa War. As the goddess of mountains in Gensokyo, she was planning to make a comeback and regain faith among humans.
To achieve her great plan, she decides to build shrines in human villages. Of course, each village can bulid at most one shrine. If she builds a shrine in the i-th village, she can get Fifaith points.
Because of geological differences and the Quantum Entanglement theory, each village has one and only one "entangled" village Pi (it is a kind of one-way relationship). If Suwakobuilds shrines both in the i-th village and the Pi-th village, the faith she get from the i-th village will changes by Gi points (it can result to a negative value of faith points). If she only builds a shrine in the Pi-th village, but not in the i-th village, the faith she get will not changes.
Now, please help Suwako to find out the maximal faith points she can get.
Input
There are multiple test cases. For each test case:
The first line contains an integer N (2 <= N <= 100000) indicates the number of villages in Gensokyo. Then followed by N lines, each line contains three integers Fi (0 <= Fi <= 100000) Gi (-100000 <= Gi <= 100000) Pi (1 <= Pi <= N and Pi will not point to the i-th village itself).
Output
For each test case, output the maximal faith points that Suwako can get.
Sample Input
23 -1 22 -2 143 -2 24 -3 32 -1 15 -2 2
Sample Output
39
Author: JIANG, Kai
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;typedef long long ll;const int MAXN=100000+100;int n,m,edge_cnt;int a[MAXN],g[MAXN],in[MAXN],head[MAXN],vis[MAXN];ll dp[MAXN][2],dp1[MAXN][2];struct Edge{ int v; int next;}edge[MAXN];void init(){ edge_cnt=0; memset(in,0,sizeof(in)); memset(dp,0,sizeof(dp)); memset(vis,0,sizeof(vis)); memset(head,-1,sizeof(head));}void addedge(int u,int v){ edge[edge_cnt].v=v; edge[edge_cnt].next=head[u]; head[u]=edge_cnt++;}ll dfs(int st,int cur,ll dp[][2],int flag){ int u=cur,v=edge[head[u]].v; while(v!=st) { dp[v][0]+=max(dp[u][0],dp[u][1]); dp[v][1]+=max(dp[u][0],dp[u][1]+g[u]); u=v; v=edge[head[u]].v; } if(flag) dp[u][1]+=g[u]; return max(dp[u][0],dp[u][1]);}ll get_ans(int u){ int v=edge[head[u]].v; dp1[v][0]+=dp[u][0]; dp1[v][1]+=dp[u][0];// u不取 ll ans1=dfs(u,v,dp1,0); dp[v][0]+=dp[u][1]; dp[v][1]+=dp[u][1]+g[u]; ll ans2=dfs(u,v,dp,1); return max(ans1,ans2);}int main(){ //freopen("text.txt","r",stdin); while(~scanf("%d",&n)) { init(); for(int i=1;i<=n;i++) { int j; scanf("%d%d%d",&a[i],&g[i],&j); addedge(i,j); in[j]++; } queue<int>Q; for(int i=1;i<=n;i++) { dp[i][1]=a[i]; if(!in[i]) Q.push(i); } while(!Q.empty()) { int u=Q.front(); Q.pop(); vis[u]=1; int v=edge[head[u]].v; dp[v][0]+=max(dp[u][0],dp[u][1]); dp[v][1]+=max(dp[u][1]+g[u],dp[u][0]); if(!(--in[v])) Q.push(v); } memcpy(dp1,dp,sizeof(dp)); ll sum=0; for(int i=1;i<=n;i++) if(!vis[i]) { sum+=get_ans(i); vis[i]=1; for(int j=edge[head[i]].v;j!=i;j=edge[head[j]].v) vis[j]=1; } printf("%lld\n",sum); } return 0;}
- zoj3527
- ZOJ3527
- 树型DP ZOJ3527 8月月赛1008 一图N个点,N条边,取某个点会有信任点,同时某个点与它的后继结点同时取的话,它的信任点会改变一个值,问怎么取点,使得总信任点最大
- [LeetCode1] Two Sum
- cocos2dx xcode5 创建项目
- SwipeListView 详解 实现微信,QQ等滑动删除效果
- 数据库访问性能优化
- http://www-cs-students.stanford.edu/~blynn/gitmagic/intl/zh_cn/index.html
- zoj3527
- 黑马程序员----Map集合
- xen
- vs2010 MFC为对话框添加背景。
- USACO numtri 数塔问题
- 最长递增子序列
- 网络基础知识--网关、DNS服务器、子网掩码
- Android Fragment的介绍与使用(案例Demo)
- Swift中创建Array和Dictionary的初始化