【tarjan缩点】hdu 3072
来源:互联网 发布:php加密代码 编辑:程序博客网 时间:2024/06/07 14:18
这题要先缩点在找没有入边的点,再找权值最小的边,注意:可能有重边,看sample3就知道!
用vector和stack的缘故,比较慢500+ms
#include <list>#include <map>#include <set>#include <queue>#include <string>#include <deque>#include <stack>#include <algorithm>#include <iostream>#include <iomanip>#include <cstdio>#include <math.h>#include <cstdlib>#include <limits.h>#include <time.h>#include <string.h>using namespace std;
#define LL long long#define PI acos(-1.0)#define FRE freopen("a.txt","r",stdin)#define MAX INT_MAX#define MIN INT_MIN#define eps 1e-10#define MOD 1000000007#define N 50005int min(int a,int b){return a<b?a:b;}struct node{ int t; int w;};int n,m;vector<node> v[N];stack<int> s;bool vis[N];bool Instack[N];int low[N],dfn[N];int belong[N];int step,t;int cost[N];
void init(){ int i; for(i=0;i<=n;i++){ v[i].clear(); vis[i]=0; low[i]=dfn[i]=0; Instack[i]=0; belong[i]=0; cost[i]=MAX; } while(!s.empty())s.pop(); t=0; step=0;}void tarjan(int u){ vis[u]=1; step++; s.push(u); Instack[u]=1; low[u]=dfn[u]=step; int i,j; for(i=0;i<v[u].size();i++){ int x=v[u][i].t; 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]){ t++; while(1){ int x=s.top(); s.pop(); belong[x]=t; //// Instack[x]=0; if(x==u)break; } }}void gao(){ int i,j; for(i=0;i<n;i++) if(!vis[i]) tarjan(i);
for(i=0;i<n;i++){ int x=belong[i]; for(j=0;j<v[i].size();j++){ int y=belong[v[i][j].t]; if(x==y)continue; else{ cost[y]=min(cost[y],v[i][j].w); } } } int ans=0; for(i=0;i<n;i++){ if(cost[i]!=MAX) ans+=cost[i]; } printf("%d\n",ans);}int main(){ while(scanf("%d%d",&n,&m)!=EOF){ int i,j; init(); while(m--){ int a,b,c; scanf("%d%d%d",&a,&b,&c); node tmp; tmp.t=b; tmp.w=c; v[a].push_back(tmp); } gao(); } return 0;}
- 【tarjan缩点】hdu 3072
- hdu 3072 Intelligence System (Tarjan+Kosaraju+缩点)
- hdu 2767 Proving Equivalences (tarjan + 缩点)
- HDU 3836 Equivalent Sets(Tarjan+缩点)
- hdu 1827 Summer Holiday tarjan+缩点
- hdu 2767 Proving Equivalences //tarjan+缩点
- hdu 3836 Equivalent Sets //tarjan+缩点
- HDU 3639 Tarjan + 缩点 + 反建图 + 搜索
- Tarjan 缩点——HDU 5934
- hdu 1827 Summer Holiday【Tarjan缩点】
- HDU 5934 Bomb(tarjan缩点)
- HDU 5934 Bomb (tarjan缩点)
- hdu 5934-建图+tarjan缩点
- HDU 3072 Intelligence System (tarjan-scc缩点 + 最小树形图)
- HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)
- hdu 3836 Equivalent Sets 强联通 tarjan缩点
- HDU 3639 Hawk-and-Chicken(Tarjan缩点+反向DFS)
- HDU 1827 Summer Holiday(Tarjan缩点)
- HDU Today
- sizeof(size_t)的疑惑||size_t sizeof long unsigned int printf max value
- YUI拖动
- Java 利用Linux Openssl 库计算文件校验码
- NDK 1253 那23个路口
- 【tarjan缩点】hdu 3072
- NDK 1254 我们的可可西里
- 让程序在崩溃时体面的退出之终极解决方案(SEH+Dump+Unhandled Exception Filter)
- SuSE linux 使用minicom
- 黑马程序员_交通灯管理系统
- NDK 1255 我们的公司
- Struts2 笔记
- POJ 2531 Network Saboteur 随机化算法/dfs
- 内存管理编程指南