poj 3204 Ikki's Story I - Road Reconstruction && zoj 2532 Internship 网络流关键边

 

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1000;
#define inf 1000000
int n,m;
int from[maxn],to[maxn];
int level[maxn],que[maxn];
int head[maxn],lon;
int num[15000];
int min(int a,int b)
{
	if(a<b) return a;
	else return b;
}
struct edge
{
    int next,from,to,c;
}e[15000];
void edgeini()
{
    memset(head,-1,sizeof(head));
    lon=-1;
}
void edgemake(int from,int to,int c)
{
    e[++lon].c=c;
    e[lon].to=to;
    e[lon].from=from;
    e[lon].next=head[from];
    head[from]=lon;
}
void make(int from,int to,int c)
{
    edgemake(from,to,c);
    edgemake(to,from,0);
}

bool makelevel(int s,int t)
{
    memset(level,0,sizeof(level));
    int front=1,end=0;
    que[++end]=s;
    level[s]=1;
    while(front<=end)
    {
        int u=que[front++];
        if(u==t) return true;
        for(int k=head[u];k!=-1;k=e[k].next)
        {
            int v=e[k].to;
            if(!level[v]&&e[k].c)
            {
                que[++end]=v;
                level[v]=level[u]+1;
            }
        }
    }
    return false;
}

int dfs(int now,int t,int maxf)
{
    if(now==t||maxf==0) return maxf;
    int ret=0;
    for(int k=head[now];k!=-1;k=e[k].next)
    {
        int u=e[k].to;
        if(level[u]==level[now]+1&&e[k].c)
        {
            int f=dfs(u,t,min(e[k].c,maxf-ret));
            e[k].c-=f;
            e[k^1].c+=f;
            ret+=f;
            if(ret==maxf) return ret;
        }
    }
    if(ret==0) level[now]=0;
    return ret;
}

int maxflow(int s,int t)
{
    int ret=0;
    while(makelevel(s,t))
    {
        ret+=dfs(s,t,inf);
    }
    return ret;
}
void dfs1(int now)
{
    int i;
    from[now]=1;
    for(i=head[now];i!=-1;i=e[i].next)
        if(e[i].c&&!from[ e[i].to ]) dfs1(e[i].to);
}
void dfs2(int now)
{
    int i;
    to[now]=1;
    for(i=head[now];i!=-1;i=e[i].next)
        if(e[i^1].c&&!to[ e[i].to ]) dfs2(e[i].to);
}
int main()
{
    scanf("%d%d",&n,&m);
    int i,j,s,t;
    s=0;
    t=n-1;
    edgeini();
    int u,v,w;
    for(i=1;i<=m;i++)
    {
        scanf("%d%d%d",&u,&v,&w);
        num[i]=lon+1;
        make(u,v,w);
    }
    int flow=maxflow(s,t);
    memset(from,0,sizeof(from));
    memset(to,0,sizeof(to));
    dfs1(s);
    dfs2(t);
    int cnt=0;
    for(i=1;i<=m;i++)
    {
        u=e[num[i]].from;
        v=e[num[i]].to;
        if( from[u]==1 && to[v]==1 && e[num[i]].c==0 )
            cnt++;
    }
    printf("%d\n",cnt);
    return 0;
}


 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1000;
#define inf 1000000
int n,m,l;
int from[maxn],to[maxn];
int level[maxn],que[maxn];
int head[maxn],lon;
int num[3000],ans[1000];
int min(int a,int b)
{
	if(a<b) return a;
	else return b;
}
struct edge
{
    int next,from,to,c;
}e[3000];
void edgeini()
{
    memset(head,-1,sizeof(head));
    lon=-1;
}
void edgemake(int from,int to,int c)
{
    e[++lon].c=c;
    e[lon].to=to;
    e[lon].from=from;
    e[lon].next=head[from];
    head[from]=lon;
}
void make(int from,int to,int c)
{
    edgemake(from,to,c);
    edgemake(to,from,0);
}

bool makelevel(int s,int t)
{
    memset(level,0,sizeof(level));
    int front=1,end=0;
    que[++end]=s;
    level[s]=1;
    while(front<=end)
    {
        int u=que[front++];
        if(u==t) return true;
        for(int k=head[u];k!=-1;k=e[k].next)
        {
            int v=e[k].to;
            if(!level[v]&&e[k].c)
            {
                que[++end]=v;
                level[v]=level[u]+1;
            }
        }
    }
    return false;
}

int dfs(int now,int t,int maxf)
{
    if(now==t||maxf==0) return maxf;
    int ret=0;
    for(int k=head[now];k!=-1;k=e[k].next)
    {
        int u=e[k].to;
        if(level[u]==level[now]+1&&e[k].c)
        {
            int f=dfs(u,t,min(e[k].c,maxf-ret));
            e[k].c-=f;
            e[k^1].c+=f;
            ret+=f;
            if(ret==maxf) return ret;
        }
    }
    if(ret==0) level[now]=0;
    return ret;
}

int maxflow(int s,int t)
{
    int ret=0;
    while(makelevel(s,t))
    {
        ret+=dfs(s,t,inf);
    }
    return ret;
}
void dfs1(int now)
{
    int i;
    from[now]=1;
    for(i=head[now];i!=-1;i=e[i].next)
        if(e[i].c&&!from[ e[i].to ]) dfs1(e[i].to);
}
void dfs2(int now)
{
    int i;
    to[now]=1;
    for(i=head[now];i!=-1;i=e[i].next)
        if(e[i^1].c&&!to[ e[i].to ]) dfs2(e[i].to);
}
int main()
{
    while(1)
    {
        int i,j,s,t;
        scanf("%d%d%d",&n,&m,&l);
        if(n==0) break;
        s=m+n+1;
        t=0;
        edgeini();
        for(i=1;i<=n;i++) make(s,i,inf);
        int u,v,w;
        for(i=1;i<=l;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            num[i]=lon+1;
            make(u,v,w);
        }
        int flow=maxflow(s,t);
        memset(from,0,sizeof(from));
        memset(to,0,sizeof(to));
        dfs1(s);
        dfs2(t);
        int cnt=0;
        for(i=1;i<=l;i++)
        {
            u=e[num[i]].from;
            v=e[num[i]].to;
            if( from[u]==1 && to[v]==1 && e[num[i]].c==0 )
                ans[cnt++]=i;
        }
        sort(ans,ans+cnt);
        if(cnt>0)
        {
            printf("%d",ans[0]);
            for(i=1;i<cnt;i++)
                printf(" %d",ans[i]);
            printf("\n");
        }
        else printf("\n");
    }
    return 0;
}


 

posted @ 2014-06-24 11:47  贝尔摩德  阅读(180)  评论(0编辑  收藏  举报