HDU6582 Path minimum cut shortest +

URL: https://vjudge.net/problem/HDU-6582

Meaning of the questions:

Given a directed acyclic FIG have multiple edges, that is, deleting the cost of a length of a side edge, such that the shortest seek point and point $ 1 $ $ $ n-minimum cost becomes long or short absence most.

answer:

For the configuration of edges in the shortest path in a configuration of a two-point, two-point communication if not in this subgraph, then the actual figures, the shortest path becomes long, or even a certain shortest absent. Therefore, seeking a point $ 1 $ to other points of the shortest path, the shortest path of all recording side establish subgraph on Qiuzi FIG minimum cut, i.e. maximum flow. For all sides, if the length of the shortest of the two end sides of the absolute value of the difference is equal to the right side, then the shortest path on this edge.

AC Code:

#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5+5;
const long long INF=0x3f3f3f3f3f3f3f3f;
const int inf=0x3f3f3f3f;
#define ll long long
struct Edge
{
    int u,v;
    long long w;
    Edge(int a,int b,long long c)
    {
        u=a,v=b,w=c;
    }
    bool operator<(const Edge &a)const
    {
        return w>a.w;
    }
};
vector<Edge>Eo[MAXN];
long long diss[MAXN];
bool vis[MAXN];
void dijkstra()
{
    memset(diss,inf,sizeof(diss));
    memset(vis,0,sizeof(vis));
    priority_queue<Edge>que;
    diss[1]=0;
    que.push(Edge(1,1,0));
    while(!que.empty())
    {
        auto tmp=que.top();
        que.pop();
        if(vis[tmp.v])
            continue;
        vis[tmp.v]=1;
        for(auto &e:Eo[tmp.v])
        {
            if(diss[e.v]>diss[tmp.v]+e.w)
            {
                diss[e.v]=diss[tmp.v]+e.w;
                if(!vis[e.v])
                    que.push(Edge(tmp.v,e.v,diss[e.v]));
            }
        }
    }
}

//最大流模板
int sp,tp;//原点、汇点 
struct node
{
	int v,next;
	ll cap;
MP} [* MAXN. 4]; 
int pre [MAXN], dis [MAXN], CUR [MAXN]; // current CUR arc optimization, dis FIG storage tier number of layers each point (i.e., the origin of the shortest distance), pre build adjacency list 
int CNT = 0; 
void the init () // Do not forget that the initialization 
{ 
	CNT = 0; 
	Memset (pre, -1, the sizeof (pre)); 
} 
void the Add (int U, V int, int w) // bordered 
{ 
	MP [CNT] .v = V; 
	MP [CNT] .cap = W; 
	MP [CNT] .next pre = [U]; 
	pre [U] = CNT ++; 
} 
BOOL BFS () / / layered construction of FIG 
{ 
	Memset (DIS, -1, the sizeof (DIS)); 
	Queue <int> Q; 
	(! q.empty ()) the while 
	q.pop (); 
	q.push (SP); 
	DIS [SP ] = 0; 
	int U, V; 
	the while (! q.empty ()) 
	{ 
		U = q.front (); 
		q.pop (); 
		for (int I = pre [U]; = I -. 1;! = I MP [I] .next) 
		{ 
			V MP = [I ] .v;
			if(dis[v]==-1&&mp[i].cap>0)
			{
				dis[v]=dis[u]+1;
				q.push(v);
				if(v==tp)
				break;
			}
		}
	}
	return dis[tp]!=-1;
}
ll dfs(int u,ll cap)//寻找增广路
{
	if(u==tp||cap==0)
	return cap;
	ll res=0,f;
	for(int &i=cur[u];i!=-1;i=mp[i].next)
	{
		int v=mp[i].v;
		if(dis[v]==dis[u]+1&&(f=dfs(v,min(cap-res,mp[i].cap)))>0)
		{
			mp[i].cap-=f;
			mp[i^1].cap+=f;
			res+=f;
			if(res==cap)
			return cap;
		}
	}
	if(!res)
	dis[u]=-1;
	return res;
}
ll dinic(int n)
{
	ll ans=0;
	while(bfs())
	{
		for(int i=1;i<=n;i++)
		    cur[i]=pre[i];
		ans+=dfs(sp,inf);
	}
	return ans;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        int n,m,a,b,c;
        cin>>n>>m;
        for(int i=1;i<=n;++i)
            Eo[i].clear();
        for(int i=0;i<m;++i)
        {
            cin>>a>>b>>c;
            Eo[a].push_back(Edge(a,b,c));
        }
        dijkstra();
        sp=1,tp=n;
        init();
        //cout<<endl;
        for(int i=1;i<=n;++i)
            for(auto &e:Eo[i])
                if(diss[e.v]-diss[e.u]==e.w)
                {
                    add(e.u,e.v,e.w);
                    add(e.v,e.u,0);
                }
        cout<<dinic(n)<<endl;
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Aya-Uchida/p/11260555.html