poj 3160【tarjan缩点+拓扑排序+DP】
2011-08-01 19:31
393 查看
http://poj.org/problem?id=3160
方法:tarjan缩点+topsort+dp,注意这句话. 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.(就是说,他从一个点出发,一直走下去,假如缩点后有多个孤立点,他只能选择其中一个)
code:
方法:tarjan缩点+topsort+dp,注意这句话. 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.(就是说,他从一个点出发,一直走下去,假如缩点后有多个孤立点,他只能选择其中一个)
code:
#include <vector> #include <list> #include <map> #include <set> #include <queue> #include <string.h> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <limits.h> using namespace std; #define LL long long #define pi acos(-1) #define N 30010 #define INF INT_MAX #define eps 1e-8 //********************************************* //poj 3160 tarjan + top //********************************************* vector<int> v ; vector<int> top ; stack<int> st; int low ,dfn ,in ,belong ; int val ,vv ,vvm ; bool inStack ,vis ; int time,num; int n,m; int min(int a,int b) { return a>b?b:a; } int max(int a,int b) { return a>b?a:b; } void tarjan(int u) { int i; vis[u]=1; st.push(u); inStack[u]=1; time++; low[u]=time,dfn[u]=time; for(i=0;i<v[u].size();i++) { int x=v[u][i]; if(!vis[x]) { tarjan(x); low[u]=min(low[u],low[x]); } else if(inStack[x]) low[u]=min(low[u],dfn[x]); } if(low[u]==dfn[u]) { num++; while(1) { int x=st.top(); st.pop(); belong[x]=num; vv[num]+=val[x]; inStack[x]=0; if(x==u)break; } } } void work() { int i,j,k; for(i=0;i<n;i++) for(j=0;j<v[i].size();j++) { int x=v[i][j]; if(belong[i]!=belong[x]) { in[belong[x]]++; top[belong[i]].push_back(belong[x]); } } queue<int> q; for(i=1;i<=num;i++) if(in[i]==0) { q.push(i); vvm[i]=vv[i]; } while(!q.empty()) { int x=q.front(); q.pop(); for(i=0;i<top[x].size();i++) { int y=top[x][i]; in[y]--; vvm[y]=max(vvm[y],vvm[x]+vv[y]); if(in[y]==0) q.push(y); } } int ans=0; for(i=1;i<=num;i++) ans=max(ans,vvm[i]); printf("%d\n",ans); } int main() {freopen("a.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF) { int i,j,k; for(i=0;i<=n;i++) { vis[i]=inStack[i]=low[i]=dfn[i]=vv[i]=vvm[i]=belong[i]=val[i]=in[i]=0; v[i].clear(); top[i].clear(); } while(!st.empty())st.pop(); for(i=0;i<n;i++) { scanf("%d",&val[i]); if(val[i]<0)val[i]=0; } while(m--) { int a,b; scanf("%d%d",&a,&b); v[a].push_back(b); } time=0,num=0; for(i=0;i<n;i++) if(!vis[i]) tarjan(i); work(); } return 0; }
相关文章推荐
- POJ 3160 Father Christmas flymouse tarjan缩点+spfa求最长路
- poj 3160 spfa+tarjan 无向图最值问题
- POJ 1236 Tarjan缩点+求度数
- 强连通分量,Tarjan,缩点(Network of Schools,POJ 1236)
- Countries in War (POJ 3114) Tarjan缩点+最短路
- [BZOJ 1051][POJ 2186] 受欢迎的牛/Popular Cows Tarjan缩点+判断出度
- poj 3160 Father Christmas flymouse(Tarjan+SPFA)
- poj 2762 tarjan缩点+拓扑序
- POJ 2186 Tarjan缩点构图+处理度数
- POJ-2186 Popular Cows,tarjan缩点找出度为0的点。
- POJ_2186 Popular Cows (Tarjan 强连通分量 缩点)
- POJ-1236(tarjan缩点)
- POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]
- POJ 2186 popular cows (tarjan + 缩点)
- poj 3592 Instantaneous Transference(tarjan + 缩点 + 最长路)
- poj 3160 Father Christmas flymouse(强连通缩点+最长路)
- POJ 1236 Network of Schools (tarjan求强连通,缩点)
- The Bottom of a Graph poj 2553 缩点+Tarjan
- POJ 2186 -- Popular Cows【强连通分支 && Tarjan缩点】
- POJ 1236 Network of Schools (Tarjan + 缩点)