HDU 5294 Tricks Device (2015 MUT#1 最短路建图+最小割)

【题目链接】:click here~~

【题目大意】:

给定一个无向图,从起点到终点,只有走最短路,才能在规定时限内到达,问最少去掉几条边使不能到达,最多去掉几条边仍能到达
【思路】最短路:寻找道路边数最少的最短路,总边数减去最少边数即为第二问答案,在最短路构成的图上每条边增加1的流量,对新图跑一遍最小割(即给定的网络,为了保证没有从s到t的路径,需要删除的边的容量的最小值,根据Ford-Fulkerson算法可以推导最小割等于最大流)即为第一问答案。
ps:如何求边数最少的最短路,我们定义dist[i]为从顶点出发的最短距离,那么如果dist[i]-dist[j]==mat1[i][j],即dist[i]恰好比dist[j]多出的距离为输入的mat1[i][j],即i,j之间的距离。即为所求。
代码:

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
#define max(a,b)((a)>(b)?(a):(b))
#define min(a,b)((a)<(b)?(a):(b))
#define CLR(a,x) memset (a,x,sizeof(a));
typedef long long LL;
const int maxm=600005;// the max edge
const int maxn=2005;// the max vertex
const int inf = 0x3f3f3f3f;
struct Edge
{
    int v,w,next;
} edge[maxm];
int head[maxn],dis[maxn];
int tot;
void add_edge(int u,int v,int w) // the Reverse Arc
{
    edge[tot].v=v,edge[tot].w=w,edge[tot].next=head[u];head[u]=tot++;
    edge[tot].v=u,edge[tot].w=0,edge[tot].next=head[v];head[v]=tot++;
}
void init()
{
    tot=0;
    CLR(head,-1);
}
bool bfs(int s,int t)
{
    CLR(dis,-1);
    queue<int>que;
    que.push(s);
    dis[s]=0;
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v;
            if(dis[v]<0&&edge[i].w>0)
            {
                dis[v]=dis[u]+1;
                que.push(v);
            }
        }
    }
    return dis[t]!=-1;
}
int dfs(int s,int t,int f)
{
    int flow;
    if(t==s) return f;
    for(int i=head[s]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(edge[i].w && (dis[v]==dis[s]+1) &&(flow =dfs(v,t,min(f,edge[i].w)))>0)
        {
            edge[i].w-=flow;
            edge[i^1].w+=flow;
            return flow;
        }
    }
    return 0;
}
int Dinic(int s,int t)
{
    int ans=0,tmp;
    while(bfs(s,t))
        while(tmp=dfs(s,t,inf))ans+=tmp;
    return ans;
}
int mat1[maxn][maxn],dist1[maxn],vis[maxn];
int mat2[maxn][maxn],dist2[maxn],num[maxn][maxn];
void dijkstra(int mat1[][maxn],int dist1[],int x,int n)
{
    int i,j,v,temp;
    for(i=0; i<n; ++i)
    {
        dist1[i]=inf;
        vis[i]=0;
    }
    dist1[x]=0;
    for(i=0; i<n; ++i)
    {
        v=-1;
        temp=inf;
        for(j=0; j<n; ++j)
        {
            if(!vis[j] && dist1[j]<temp)
            {
                temp=dist1[v=j];
            }
        }
        if(v==-1) break;
        vis[v]=1;
        for(j=0; j<n; ++j)
        {
            if(!vis[j] && dist1[v]+mat1[v][j]<dist1[j])
            {
                dist1[j]=dist1[v]+mat1[v][j];
            }
        }
    }
}
inline LL read()
{
    int c=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){c=c*10+ch-'0';ch=getchar();}
    return c*f;
}
int main()
{
    int t,m,n,u,v,w;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        CLR(mat1,inf);CLR(mat2,inf);CLR(num,0);
        for(int i=0; i<=n; ++i) mat1[i][i]=0;
        for(int i=1; i<=m; ++i)
        {
            u=read();v=read();w=read();u--;v--;
            if(mat1[u][v]>w)
            {
                mat1[u][v]=mat1[v][u]=w;
                num[u][v]=num[v][u]=1;
            }
            else if(mat1[u][v]==w)
            {
                num[u][v]++;num[v][u]++;
            }
        }
        dijkstra(mat1,dist1,0,n);
        for(int i=0; i<=n; ++i) mat2[i][i]=0;
        for(int i=0; i<n; ++i)// find the shortest path
        {
            for(int j=0; j<n; ++j)
            {
                if(i!=j)
                    if(dist1[i]-dist1[j]==mat1[i][j])
                    {
                        add_edge(j,i,num[i][j]);
                        mat2[j][i]=1;
                    }
            }
        }
        dijkstra(mat2,dist2,0,n);
        int ans=Dinic(0,n-1);
        printf("%d %d\n",ans,m-dist2[n-1]);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值