HDU多校--Path--最短路存圖+網絡流

Path

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3941    Accepted Submission(s): 1136


 

Problem Description

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.

 

 

Input

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 .

 

 

Output

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

 

 

Sample Input


 

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

 

 

Sample Output


 

3

 

 

Source

2019 Multi-University Training Contest 1

 

 

Recommend

 

https://blog.csdn.net/lanshan1111/article/details/100934871題意、解法基本一樣。

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 5;
typedef pair<ll, int> P;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, cas;

ll d[maxn];
struct GDJ
{
    int head[maxn], cnt;
    int to[maxn << 1], nxt[maxn << 1];
    ll val[maxn << 1];

    void init()
    {
        memset(head, -1, (n + 5) * sizeof(int));
        cnt = -1;
    }

    void ade(int a, int b, ll c)
    {
        to[++cnt] = b;
        nxt[cnt] = head[a];
        val[cnt] = c;
        head[a] = cnt;
    }

    bool vis[maxn];
    void dj(int s)
    {
        priority_queue<P, vector<P>, greater<P> > que;
        memset(d, 0x3f, (n + 5) * sizeof(ll));
        memset(vis, 0, (n + 5) * sizeof(bool));
        d[s] = 0;
        que.push(P(0, s));
        while(!que.empty())
        {
            P p = que.top();
            que.pop();
            int u = p.second;
            if(vis[u])
                continue;
            vis[u] = 1;
            for(int i = head[u]; ~i; i = nxt[i])
            {
                int v = to[i];
                if(d[v] > d[u] + val[i])
                {
                    d[v] = d[u] + val[i];
                    que.push(P(d[v], v));
                }
            }
        }
    }
} DJ;

struct GDC
{
    int depth[maxn], cur[maxn], head[maxn], cnt;
    int to[maxn << 1], nxt[maxn << 1];
    ll val[maxn << 1];

    void init()
    {
        memset(head, -1, (n + 5) * sizeof(ll));
        cnt = -1;
    }

    void ade(int a, int b, ll c)
    {
        to[++cnt] = b;
        nxt[cnt] = head[a];
        val[cnt] = c;
        head[a] = cnt;
    }

    bool bfs()
    {
        queue<int> que;
        que.push(1);
        memset(depth, 0, (n + 5) * sizeof(int));
        depth[1] = 1;
        while(!que.empty())
        {
            int u = que.front();
            que.pop();
            for(int i = head[u]; ~i; i = nxt[i])
            {
                if(val[i] > 0 && depth[to[i]] == 0)
                {
                    depth[to[i]] = depth[u] + 1;
                    que.push(to[i]);
                }
            }
        }
        if(depth[n])
            return 1;
        else
            return 0;
    }

    ll dfs(int u, ll dist)
    {
        if(u == n)
            return dist;
        for(int &i = cur[u]; ~i; i = nxt[i])
        {
            if(depth[to[i]] == depth[u] + 1 && val[i] > 0)
            {
                ll tmp = dfs(to[i], min(dist, val[i]));
                if(tmp > 0)
                {
                    val[i] -= tmp;
                    val[i ^ 1] += tmp;
                    return tmp;
                }
            }
        }
        return 0;
    }

    ll dinic()
    {
        ll res =0, d;
        while(bfs())
        {
            for(int i = 0; i <= n; i ++)
                cur[i] = head[i];
            while(d = dfs(1,INF))
                res += d;
        }
        return res;
    }
} DC;

int main()
{
    scanf("%d",&cas);
    while(cas --)
    {
        scanf("%d %d",&n,&m);
        DJ.init();
        for(int i = 1; i <= m; i++)
        {
            int a, b;
            ll c;
            scanf("%d %d %d",&a,&b,&c);
            DJ.ade(a, b, c);
        }

        DJ.dj(1);
        //	cout << "              " << d[n] << endl;
        //	for(int i = 1; i <= n; i ++) cout << d[i] << endl;
        if(d[n] == INF)
        {
            printf("0\n");
           //cout << 0 << endl;
            continue;
        }
        DC.init();//跑最短路
        for(int u = 1; u <= n; u ++)
        {
            for(int i = DJ.head[u]; ~i; i = DJ.nxt[i])
            {
                int v = DJ.to[i];
                ll cst = DJ.val[i];
                if(d[v] == DJ.val[i] + d[u])
                {
                    //	cout << u << " " << v << endl;
                    DC.ade(u, DJ.to[i], cst), DC.ade(DJ.to[i],u,0);
                }
            }
        }
        printf("%lld\n",DC.dinic());
       // cout << DC.dinic() << endl;//跑最大流
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章