POJ 3160 Father Christmas flymouse(强连通分量+spfa最长路)
来源:互联网 发布:狗爹怎么解析域名 编辑:程序博客网 时间:2024/05/22 03:07
最近老是莫名其妙1A。
//// main.cpp// Richard//// Created by 邵金杰 on 16/8/18.// Copyright © 2016年 邵金杰. All rights reserved.//#include<iostream>#include<cstdio>#include<vector>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int maxn=30000+100;int dfn[maxn],low[maxn],belong[maxn],w[maxn],instack[maxn],in[maxn],dist[maxn],tree[maxn];vector<vector<int> > G(maxn);vector<vector<int> > map(maxn);vector<int> s;int n,m;int color,idex;void tarjan(int u){ dfn[u]=low[u]=++idex; s.push_back(u); instack[u]=1; for(int i=0;i<G[u].size();i++) { int v=G[u][i]; if(!dfn[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v]) { low[u]=min(low[u],dfn[v]); } } if(dfn[u]==low[u]) { int p; color++; int sum=0; do{ p=s[s.size()-1]; instack[p]=0; sum+=w[p]; belong[p]=color; s.pop_back(); }while(u!=p); tree[color]=sum; }}void creat_graph(){ for(int i=0;i<n;i++) { for(int j=0;j<G[i].size();j++) { int v=G[i][j]; if(belong[i]!=belong[v]) { in[belong[v]]++; map[belong[i]].push_back(belong[v]); } } } for(int i=1;i<=color;i++) { if(in[i]==0) {map[color+1].push_back(i);} }}int spfa(){ memset(dist,0,sizeof(dist)); queue<int> q; q.push(color+1); while(!q.empty()) { int s=q.front(); q.pop(); for(int i=0;i<map[s].size();i++) { int e=map[s][i]; if(dist[e]<dist[s]+tree[e]) { dist[e]=dist[s]+tree[e]; q.push(e); } } } sort(dist+1,dist+color+2); return dist[color+1];}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { color=0,idex=0; for(int i=0;i<n;i++) G[i].clear(),map[i].clear(); map[n].clear(); s.clear(); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(belong,0,sizeof(belong)); memset(w,0,sizeof(w)); memset(instack,0,sizeof(instack)); memset(in,0,sizeof(in)); memset(dist,0,sizeof(dist)); memset(tree,0,sizeof(tree)); for(int i=0;i<n;i++){ scanf("%d",&w[i]); if(w[i]<0) w[i]=0; } int a,b; for(int i=0;i<m;i++) { scanf("%d%d",&a,&b); G[a].push_back(b); } for(int i=0;i<n;i++) { if(!dfn[i]) { tarjan(i); } } creat_graph(); printf("%d\n",spfa()); } return 0;}
0 0
- POJ 3160 Father Christmas flymouse(强连通分量+spfa最长路)
- POJ - 3160 Father Christmas flymouse(最长路+强连通分量)
- 【POJ】3160 Father Christmas flymouse 强连通+最长路
- POJ 3160 Father Christmas flymouse 强连通+最长路
- poj 3160 Father Christmas flymouse (强连通分量+记忆化搜素)
- poj 3160 Father Christmas flymouse 强连通分量
- POJ 3160 — Father Christmas flymouse 强连通+spfa
- POJ 3160 Father Christmas flymouse 强连通+Spfa
- poj 3160-Father Christmas flymouse-强连通
- POJ 3160 Father Christmas flymouse 强连通缩点+spfa最长路
- 【连通图|强连通分量+dfs】POJ-3160 Father Christmas flymouse
- POJ 3160 Father Christmas flymouse 强连通分量+缩点+DP
- Poj 3160 Father Christmas flymouse【强连通Tarjan+SPFA+超级源点】
- poj 3160 Father Christmas flymouse (spfa + 强联通)
- poj 3160 Father Christmas flymouse(强连通+dp)
- POJ 3160 Father Christmas flymouse(强连通+DP)
- poj 3160 Father Christmas flymouse 强连通+dp
- poj 3160 Father Christmas flymouse(强连通缩点+最长路)
- 哈理工OJ 1997 又是一个神奇的布尔矩阵(判断)
- 递归
- NBUT 1670 字符串(strstr函数的处理)
- git pull 代码时报错
- servlet的执行过程
- POJ 3160 Father Christmas flymouse(强连通分量+spfa最长路)
- Unity3D各平台Application.xxxPath的路径
- SDUT(3377)数据结构实验之查找五:平方之哈希表
- UNIX编程初遇问题ourhdr.h文件
- 生产者消费者Java实现
- servlet开发细节
- Catch That Cow
- hive表属性操作
- Netty In Action中文版