POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]
2016-11-03 12:43
387 查看
Father Christmas flymouse
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
Sample Output
Hint
32-bit signed integer type is capable of doing all arithmetic.
Source
POJ Monthly--2006.12.31, Sempr
题意:多了每个节点分配一个权值,走一条路权值最大
还是求SCC缩点DP
很多题解用了spfa求DAG的最长路也可以
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 3241 | Accepted: 1099 |
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
题意:多了每个节点分配一个权值,走一条路权值最大
还是求SCC缩点DP
很多题解用了spfa求DAG的最长路也可以
//16ms #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define C(x) memset(x,0,sizeof(x)) using namespace std; const int N=30005,M=150005; inline int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } int n,m,u,v,w ; struct edge{ int v,ne; }e[M]; int h ,cnt=0; inline void ins(int u,int v){ cnt++; e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt; } int dfn ,low ,belong ,dfc,scc,val ; int st ,top; void dfs(int u){ dfn[u]=low[u]=++dfc; st[++top]=u; for(int i=h[u];i;i=e[i].ne){ int v=e[i].v; if(!dfn[v]){ dfs(v); low[u]=min(low[u],low[v]); }else if(!belong[v]) low[u]=min(low[u],dfn[v]); } if(low[u]==dfn[u]){ scc++; while(true){ int x=st[top--]; belong[x]=scc; if(w[x]>0) val[scc]+=w[x]; if(x==u) break; } } } void SCC(){ dfc=scc=0; C(dfn);C(low);C(belong);C(val); top=0; for(int i=1;i<=n;i++) if(!dfn[i]) dfs(i); } edge es ; int hs ,cs=0; inline void inss(int u,int v){ cs++; es[cs].v=v;es[cs].ne=hs[u];hs[u]=cs; } void buildGraph(){ cs=0; C(hs); for(int u=1;u<=n;u++){ int a=belong[u]; for(int i=h[u];i;i=e[i].ne){ int b=belong[e[i].v]; if(a!=b)inss(a,b); } } } int f ; int dp(int u){ if(f[u]!=-1) return f[u]; f[u]=val[u]; int mx=0; for(int i=hs[u];i;i=es[i].ne){ int v=es[i].v;//printf("dp %d v %d\n",u,v); mx=max(mx,dp(v)); } return f[u]+=mx; } int main(int argc, const char * argv[]) { while(scanf("%d%d",&n,&m)!=EOF){ for(int i=1;i<=n;i++) w[i]=read(); cnt=0;memset(h,0,sizeof(h)); for(int i=1;i<=m;i++){u=read()+1;v=read()+1;ins(u,v);} SCC(); buildGraph(); memset(f,-1,sizeof(f)); int ans=0; for(int i=1;i<=scc;i++){ if(f[i]==-1) dp(i); ans=max(ans,f[i]); } printf("%d\n",ans); } return 0; }
相关文章推荐
- poj 3160 Father Christmas flymouse(强连通缩点+最长路)
- POJ - 3160 Father Christmas flymouse(最长路+强连通分量)
- POJ 3160 强联通缩点+dp
- poj 3160 (强连通缩点&简单dp)
- POJ 3160(缩点+spfa最长路+dp)
- poj 3160 Father Christmas flymouse 【SCC缩点 + 虚拟源点SPFA求最长路】
- POJ 3160 Father Christmas flymouse Tarjon+DP
- poj 3160 Father Christmas flymouse 强连通分量
- POJ 3160 Father Christmas flymouse tarjan缩点+spfa求最长路
- poj 3160(强连通缩点 + dp)
- POJ 3160 缩点+拓扑排序+简单dp
- POJ 3160 Father Christmas flymouse 强连通分量+缩点+DP
- POJ 3160 Father Christmas flymouse 笔记
- POJ 2186(有向图的强连通分量)[改][Template:SSC/缩点]
- [SDOI2010]BZOJ 1924所驼门王的宝藏-强连通分量-缩点-拓扑排序-dp
- POJ-1236-Network of Schools【强连通分量】【缩点】
- POJ 1236 Network of Schools【强连通分量分解&&缩点||tarjan&&缩点】
- poj 3592 强连通分量+最长路(spfa或者dp)(Instantaneous Transference)
- POJ 1236 Network of Schools (强连通分量,块,缩点)
- POJ1236Network of Schools(强连通分量 + 缩点)