多校1007(最短路+最小割)

Tricks Device

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1399    Accepted Submission(s): 335


Problem Description
Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the entrance of the tomb while Dumb Zhang’s at the end of it. The tomb is made up of many chambers, the total number is N. And there are M channels connecting the chambers. Innocent Wu wants to catch up Dumb Zhang to find out the answers of some questions, however, it’s Dumb Zhang’s intention to keep Innocent Wu in the dark, to do which he has to stop Innocent Wu from getting him. Only via the original shortest ways from the entrance to the end of the tomb costs the minimum time, and that’s the only chance Innocent Wu can catch Dumb Zhang.
Unfortunately, Dumb Zhang masters the art of becoming invisible(奇门遁甲) and tricks devices of this tomb, he can cut off the connections between chambers by using them. Dumb Zhang wanders how many channels at least he has to cut to stop Innocent Wu. And Innocent Wu wants to know after how many channels at most Dumb Zhang cut off Innocent Wu still has the chance to catch Dumb Zhang.
 

Input
There are multiple test cases. Please process till EOF.
For each case,the first line must includes two integers, N(<=2000), M(<=60000). N is the total number of the chambers, M is the total number of the channels.
In the following M lines, every line must includes three numbers, and use ai、bi、li as channel i connecting chamber ai and bi(1<=ai,bi<=n), it costs li(0<li<=100) minute to pass channel i.
The entrance of the tomb is at the chamber one, the end of tomb is at the chamber N.
 

Output
Output two numbers to stand for the answers of Dumb Zhang and Innocent Wu’s questions.
 

Sample Input
  
  
8 9 1 2 2 2 3 2 2 4 1 3 5 3 4 5 4 5 8 1 1 6 2 6 7 5 7 8 1
 

Sample Output
  
  
2 6

题意就是给你一个图,求出其中所有的最短路,人只能在这些最短路中走,要求问两个数,第一个是最少去掉多少条路就不能从1走到n,第二个是问最多去掉多少路仍能从1走到n(包括那些非最短路)。很明显,先找出所有最短路,然后标记长度为路的重数,求一个最大流,也就是最小割。然后标记为1,求一条最短路。

先走一遍迪杰斯特拉,找出所有最短路,标记。然后跑一边网络流,最后跑一遍迪杰斯特拉,就好了

从无限tle到无限wa,。真是醉了

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#define debug 0
#define inf 1000000000
using namespace std;
int a[2010][2010],used[2010],c[2010][2010],d[2010][2010];
int mi=inf;
int ans1=inf,ans2=inf;
vector<int >mp[2010];

int dis[2010];
#define MAXM 60010
#define MAXN 2010
int n, m;    //点数,边数

int u, v, w; //两点及边权
struct Edge  //定义边
{
	int v, w, next;
}edge[2 * MAXM + 2 * MAXN];

int e, head[MAXN + 2], q[MAXN + 2];

///加边
void add(int u, int v, int w)
{
	edge[e].v = v;
	edge[e].w = w;
	edge[e].next = head[u];
	head[u] = e++;
}

void dij(int xx[][2010])
{
    for(int i=1; i<=n; i++)
    {
        if(xx[1][i]==0)
            dis[i]=inf;
        else dis[i]=xx[1][i];
    }
    dis[1]=0;
    memset(used,0,sizeof(used));
    used[1]=1;
    int mii,k;
    for(int i=1; i<n; i++)
    {
        mii=inf;
        for(int j=1; j<=n; j++)
        {
            if(dis[j]<mii&&used[j]==0)
            {
                mii=dis[j];
                k=j;
            }
        }
        used[k]=1;
        for(int j=1; j<=n; j++)
        {
            if(xx[k][j]!=0&&dis[k]+xx[k][j]<dis[j])
                dis[j]=dis[k]+xx[k][j];
        }
    }
    mi=dis[n];
}

///初始化
void init()
{
	e = 0;
	memset(head, -1, sizeof(head));
}

///分层
bool bffs()
{
	memset(dis, 0, sizeof(dis));
	dis[1] = 1;

	int top, tail;
	top = tail = 0;

	q[tail++] = 1;

	while (top != tail)
	{
		int curs = q[top++];

		for (int i = head[curs]; i != -1; i = edge[i].next)
		{
			int v = edge[i].v;

			if (dis[v] == 0 && edge[i].w)
			{
				dis[v] = dis[curs] + 1;
				q[tail++] = v;
				if (v == n) return true;
			}
		}
	}

	return false;
}

///找最大流
int dffs(int u, int f)
{
	if (u == n || f == 0) return f;

	int flow = 0;

	for (int i = head[u]; i != -1; i = edge[i].next)
	{
		int v = edge[i].v;

		if (dis[v] == dis[u] + 1 && edge[i].w)
		{
			int t = min(edge[i].w, f - flow);
			int inc = dffs(v, t);

			flow += inc;
			edge[i].w -= inc;
			edge[i ^ 1].w += inc;
			if (!(f - flow)) break;
		}
	}

	if (!flow) dis[u] = -1;

	return flow;
}

///dinic算法
int dinic()
{
	int Maxflow = 0;

	while (bffs()) Maxflow += dffs(1, inf);

	return Maxflow;
}

int main()
{
    int m,x,y,z;
   // if (debug)
      //  freopen("1007.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF)
    {


        for(int i=1; i<=n; i++)
            mp[i].clear();
       // mmax=0;
        ans1=inf,ans2=0;
      //  memset(a,0,sizeof(a));
        memset(used,0,sizeof(used));
        memset(c,0,sizeof(c));
        memset(d,0,sizeof(d));
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            a[i][j]=a[j][i]=inf;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            if(x==y) continue;

                if(a[x][y]==z)
                {
                    c[x][y]++;
                    c[y][x]++;
                }
                else if(a[x][y]>z)
                {
                    c[x][y]=c[y][x]=1;
                    a[x][y]=a[y][x]=z;
                }

        }

        dij(a);
        init();

        for(int tmp=1;tmp<=n;tmp++){
            for(int tt=1;tt<=n;tt++){
                if((a[tmp][tt]+dis[tmp])==dis[tt])
                {
                   // for(int j=0;j<c[tmp][tt];j++){
                        add(tmp,tt,c[tmp][tt]);
                    add(tt,tmp,0);
                  //  }
                    d[tmp][tt]=1;
                }
            }

        }


        dij(d);
        ans2=m- mi;
        ans1=dinic();

        printf("%d %d\n",ans1,ans2);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值