杭电2019多校第一场 HDU——6582 Path(最短路最大流(最短路最小割))

时间限制: 1 Sec  内存限制: 128 MB

题目描述

 

Years later, Jerry fell in love with a girl, and he often walks for a long time to pay visits to her. But, because he spends too much time with his girlfriend, Tom feels neglected and wants to prevent him from visiting her.
After doing some research on the neighbourhood, Tom found that the neighbourhood consists of exactly n houses, and some of them are connected with directed road. To visit his girlfriend, Jerry needs to start from his house indexed 1 and go along the shortest path to hers, indexed n. 
Now Tom wants to block some of the roads so that Jerry has to walk longer to reach his girl's home, and he found that the cost of blocking a road equals to its length. Now he wants to know the minimum total cost to make Jerry walk longer.
Note, if Jerry can't reach his girl's house in the very beginning, the answer is obviously zero. And you don't need to guarantee that there still exists a way from Jerry's house to his girl's after blocking some edges.

 

 

输入

The input begins with a line containing one integer T(1≤T≤10), the number of test cases.
Each test case starts with a line containing two numbers n,m(1≤n,m≤10000), the number of houses and the number of one-way roads in the neighbourhood.
m lines follow, each of which consists of three integers x,y,c(1≤x,y≤n,1≤c≤109), denoting that there exists a one-way road from the house indexed x to y of length c.

 

输出

Print T lines, each line containing a integer, the answer.

 

样例输入

复制样例数据

1
3 4
1 2 1
2 3 1
1 3 2
1 3 3

样例输出

3

题意:给出n个点,m条有向边,每条边有价值,1个人从1到n走最短路,另一个人用最小的价值割边不让他走最短路,让他走更长的路。

题解:更长不是最长,所以只要让他没法走最短路即可,这样就成了,最短路最小割,即最短路最大流,最短路最大流模板题,不解释~~

上代码:

#include 
#include 
#include 
#include 
#include 
#include 
#define ll long long
using namespace std;
const ll INF = 1e17;
const int MAXN = 40005;
int N,M;
int u,v;
ll w;
struct Edge{
    int to,rev;
    ll value;
    Edge() {}
    Edge(int a,ll b,int c):to(a),value(b),rev(c) {}
};
struct node1{
    int v,next;
    ll w;
}edge[MAXN];
vector E[MAXN];
int deep[MAXN];
int iter[MAXN],head[MAXN];
int id;
void add(int u,int v,ll w){
    edge[id].v=v;
    edge[id].w=w;
    edge[id].next=head[u];
    head[u]=id++;
}
inline void Add(int from,int to,ll value){
    E[from].push_back(Edge(to,value,E[to].size()));
    E[to].push_back(Edge(from,0,E[from].size()-1));
}
 
bool BFS(int root,int target) {
    memset(deep,-1,sizeof deep);
    queue Q;
    deep[root] = 0;
    Q.push(root);
    while(!Q.empty())
    {
        int t = Q.front();
        Q.pop();
        for(int i=0 ; i 0 && deep[E[t][i].to] == -1)
            {
                deep[E[t][i].to] = deep[t] + 1;
                Q.push(E[t][i].to);
            }
        }
    }
    return deep[target] != -1;
}
 
ll DFS(int root,int target,ll flow){
    if(root == target)return flow;
    for(int &i=iter[root] ; i0 && deep[E[root][i].to] == deep[root]+1)
        {
            ll nowflow = DFS(E[root][i].to,target,min(flow,E[root][i].value));
            if(nowflow > 0)
            {
                E[root][i].value -= nowflow;
                E[E[root][i].to][E[root][i].rev].value += nowflow;
                return nowflow;
            }
        }
    }
    return 0;
}
 
bool vis[MAXN];
ll dist[MAXN];
void spfa(int s){
    memset(vis,0,sizeof(vis));
    for(int i=0;i<=N+10;i++) dist[i]=INF;
    vis[s]=1;
    dist[s]=0;
    queueq;
    q.push(s);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=0;
        //if(u==N) break;
        for(int i=head[u]; i!=-1; i=edge[i].next){
            int v=edge[i].v;
            if (dist[v]>dist[u]+edge[i].w){
                dist[v]=dist[u]+edge[i].w;
                if (!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
ll Dinic(int root,int target){
    ll sumflow = 0;
    while(BFS(root,target))    {
        memset(iter,0,sizeof iter);
        ll mid;
        while((mid=DFS(root,target,INF)) > 0)sumflow += mid;
    }
    return sumflow;
}
void init(){
    memset(head,-1,sizeof(head));
    id=0;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&N,&M);
        init();
        for(int i=0 ; i< M ; i++){
            scanf("%d %d %lld",&u,&v,&w);
            add(u,v,w);
        }
        spfa(1);
        for(int i = 1; i <= N; ++i) {
            for(int j = head[i]; ~j; j = edge[j].next) {
                int v = edge[j].v;
                if(dist[v] - dist[i] == edge[j].w) {
                        Add(i, v, edge[j].w);
                }
            }
        }
        printf("%lld\n",Dinic(1,N));
        for (int i = 0; i <= 11000;i++) E[i].clear();
    }
    return 0;
}
 

 

你可能感兴趣的:(网络流)