hdu 1827 Summer Holiday && 3072 Intelligence System
2014-10-14 17:08
405 查看
强联通的缩点问题。
归为模板题。
hdu 1872:
代码:
hdu 3072 :
代码 :
#include<stdio.h>
#include<string.h>
#include<limits.h>
const int mx1=50005;
const int mx2=100005;
int n,m,index,cnt,res,top;
int dfn[mx1],low[mx1],vis[mx1];
int color[mx1],stack[mx1];
int to[mx1],val[mx1];
struct node
{
int s,e,c;
int next;
} edge[mx2];
int head[mx2];
void init()
{
index=top=cnt=res=0;
for(int i=0; i<mx1; i++)
{
dfn[i]=low[i]=vis[i]=color[i]=to[i]=0;
val[i]=INT_MAX;
}
memset(head,-1,sizeof(head));
}
void add(int a,int b,int c)
{
edge[res].s=a;
edge[res].e=b;
edge[res].c=c;
edge[res].next=head[a];
head[a]=res++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++index;
stack[top++]=u;
vis[u]=1;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
//printf("*************%d \n",u);
int v=edge[i].e;
if(dfn[v]==0)
{
tarjan(v);
low[u]=low[u]<low[v]?low[u]:low[v];
}
else if(vis[v]&&low[u]>dfn[v])
low[u]=dfn[v];
}
if(low[u]==dfn[u])
{
cnt++;
int v;
do
{
v=stack[--top];
vis[v]=0;
color[v]=cnt;
}
while(v!=u);
}
}
int sove()
{
for(int i=1; i<=n; i++)
{
if(dfn[i]==0)
tarjan(i);
}
int sum=0;
for(int i=1; i<=n; i++)
{
for(int j=head[i]; j!=-1; j=edge[j].next)
{
int v=edge[j].e;
if(color[i]!=color[v]&&val[color[v]]>edge[j].c)
val[color[v]]=edge[j].c;
}
}
for(int i=1; i<=cnt; i++)
{
if(val[i]<INT_MAX)
sum+=val[i];
}
printf("%d\n",sum);
}
int main()
{
//freopen("a.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
init();
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a+1,b+1,c);
}
sove();
}
return 0;
}
归为模板题。
hdu 1872:
代码:
#include<stdio.h> #include<string.h> #include<vector> #include<climits> using namespace std; const int mx=2001; int n,m; int top,cnt,index,res; int low[mx],dfn[mx],vis[mx]; int color[mx],stack[mx]; int price[mx],to[mx]; struct node { int s,e; int next; } edge[mx]; int head[mx]; void init() { index=top=cnt=res=0; for(int i=0; i<mx; i++) { low[i]=dfn[i]=vis[i]=color[i]=to[i]=0; head[i]=-1; } } void add(int a,int b) { edge[res].s=a; edge[res].e=b; edge[res].next=head[a]; head[a]=res++; } void tarjan(int u) { low[u]=dfn[u]=++index; vis[u]=1; stack[top++]=u; for(int j=head[u]; j!=-1; j=edge[j].next) { int v=edge[j].e; if(dfn[v]==0) { tarjan(v); low[u]=low[u]<low[v]?low[u]:low[v]; } if(vis[v]&&dfn[v]<low[u]) low[u]=dfn[v]; } if(dfn[u]==low[u]) { cnt++; int v; do { v=stack[--top]; vis[v]=0; color[v]=cnt; } while(v!=u); } } void sove() { for(int i=1; i<=n; i++) { if(dfn[i]==0) tarjan(i); } for(int i=0; i<res; i++) { int a=edge[i].s; int b=edge[i].e; if(color[a]!=color[b]) { to[color[b]]++; } } int sum=0,count=0; for(int i=1; i<=cnt; i++) { if(to[i]==0) { int mn=INT_MAX; for(int j=1; j<=n; j++) { if(color[j]==i&&price[j]<mn) mn=price[j]; } count++; sum+=mn; } } printf("%d %d\n",count,sum); } int main() { //freopen("a.txt","r",stdin); while(~scanf("%d%d",&n,&m)) { init(); for(int i=1; i<=n; i++) scanf("%d",&price[i]); while(m--) { int a,b; scanf("%d%d",&a,&b); add(a,b); } sove(); } return 0; }
hdu 3072 :
代码 :
#include<stdio.h>
#include<string.h>
#include<limits.h>
const int mx1=50005;
const int mx2=100005;
int n,m,index,cnt,res,top;
int dfn[mx1],low[mx1],vis[mx1];
int color[mx1],stack[mx1];
int to[mx1],val[mx1];
struct node
{
int s,e,c;
int next;
} edge[mx2];
int head[mx2];
void init()
{
index=top=cnt=res=0;
for(int i=0; i<mx1; i++)
{
dfn[i]=low[i]=vis[i]=color[i]=to[i]=0;
val[i]=INT_MAX;
}
memset(head,-1,sizeof(head));
}
void add(int a,int b,int c)
{
edge[res].s=a;
edge[res].e=b;
edge[res].c=c;
edge[res].next=head[a];
head[a]=res++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++index;
stack[top++]=u;
vis[u]=1;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
//printf("*************%d \n",u);
int v=edge[i].e;
if(dfn[v]==0)
{
tarjan(v);
low[u]=low[u]<low[v]?low[u]:low[v];
}
else if(vis[v]&&low[u]>dfn[v])
low[u]=dfn[v];
}
if(low[u]==dfn[u])
{
cnt++;
int v;
do
{
v=stack[--top];
vis[v]=0;
color[v]=cnt;
}
while(v!=u);
}
}
int sove()
{
for(int i=1; i<=n; i++)
{
if(dfn[i]==0)
tarjan(i);
}
int sum=0;
for(int i=1; i<=n; i++)
{
for(int j=head[i]; j!=-1; j=edge[j].next)
{
int v=edge[j].e;
if(color[i]!=color[v]&&val[color[v]]>edge[j].c)
val[color[v]]=edge[j].c;
}
}
for(int i=1; i<=cnt; i++)
{
if(val[i]<INT_MAX)
sum+=val[i];
}
printf("%d\n",sum);
}
int main()
{
//freopen("a.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
init();
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a+1,b+1,c);
}
sove();
}
return 0;
}
相关文章推荐
- hdu 3639 Hawk-and-Chicken (强连通 + 反图 + 缩点) && hdu1827 Summer Holiday && hdu 1269 迷宫城堡 && hdu3072 Intelligence System
- HDU 3934 Summer holiday & Poj 2079 Triangle(凸包最大内接三角形)
- HDU-4118-Holiday's Accommodation
- HDU 3072--Intelligence System【SCC缩点新构图 && 求连通所有SCC的最小费用】
- HDU 1827&&3072 强连通分量 解题报告
- hdu 4118 Holiday's Accommodation
- hdu 4118 Holiday's Accommodation 树上统计
- HDU 4118 Holiday's Accommodation (2011年成都赛区现场赛H题)
- UVA 1669 && HDU 4118 Holiday's Accommodatio (思路题目--统计子树结点 )
- HDU Summer Holiday
- hdu Summer Holiday(tarjan)
- HDU - 4118 Holiday's Accommodation
- HDU 1827 3072强联通分量
- HDU 4118--Holiday's Accommodation(树形dp)
- hdu Holiday's Accommodation dfs
- HDU 3072--Intelligence System【SCC缩点新构图 && 求连通全部SCC的最小费用】
- HDU4118:Holiday's Accommodation(思维 & dfs)
- HDU - 4118 Holiday's Accommodation
- hdu 3415 Max Sum of Max-K-sub-sequence 单调队列 求连续l(1<=l<=k)个数的和的最大值 数列可循环
- HDU 3072 //图的强连通性,缩点后求树形图 //kosaraju算法