HDU 5889 Barricade(最短路spfa+最大流dinic)

来源:互联网 发布:数据接口类型有哪些 编辑:程序博客网 时间:2024/05/29 17:56

Barricade

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1297 Accepted Submission(s): 394

Problem Description
The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as N towns and M roads, and each road has the same length and connects two towns. The town numbered 1 is where general’s castle is located, and the town numbered N is where the enemies are staying. The general supposes that the enemies would choose a shortest path. He knows his army is not ready to fight and he needs more time. Consequently he decides to put some barricades on some roads to slow down his enemies. Now, he asks you to find a way to set these barricades to make sure the enemies would meet at least one of them. Moreover, the barricade on the i-th road requires wi units of wood. Because of lacking resources, you need to use as less wood as possible.

Input
The first line of input contains an integer t, then t test cases follow.
For each test case, in the first line there are two integers N(N≤1000) and M(M≤10000).
The i-the line of the next M lines describes the i-th edge with three integers u,v and w where 0≤w≤1000 denoting an edge between u and v of barricade cost w.

Output
For each test cases, output the minimum wood cost.

Sample Input
1
4 4
1 2 1
2 4 2
3 1 3
4 3 4

Sample Output
4
【题目分析】现在一个空图上跑一次最短路,把最短路找出来,然后根据求得的最短路构建新的图,在新图上跑一遍最大流即可。

【AC代码】

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<queue>#define INF 0x3fffffffusing namespace std;#define N 1005#define M 10005struct Node{    int v,w;    Node(int vv,int ww):v(vv),w(ww){};};vector<Node>e[N];int s,t,n,m,vs,vt;int d[N];int vis[N];void spfa()//在图上跑一次最短路{    memset(vis,0,sizeof(vis));    for(int i = 1;i<=n;i++)        d[i]=INF;    d[s]=0;    queue<int>q;    q.push(s);    while(!q.empty())    {        int u = q.front();        q.pop();        vis[u]=0;        for(int i = 0;i<e[u].size();i++)        {            int v = e[u][i].v;            if(d[v]>d[u]+1)            {                d[v]=d[u]+1;                if(!vis[v])                    q.push(v);                vis[v]=1;            }        }    }}struct Edge{    int from,to,cap,flow;    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}};struct Dinic{    int s,t;    vector<Edge>edges;    vector<int> G[N];    bool vis[N];    int d[N];    int cur[N];    void init()    {       for (int i=0;i<=n+1;i++)           G[i].clear();       edges.clear();    }    void AddEdge(int from,int to,int cap)    {        edges.push_back(Edge(from,to,cap,0));        edges.push_back(Edge(to,from,0,0));        int mm=edges.size();        G[from].push_back(mm-2);        G[to].push_back(mm-1);    }    bool BFS()    {        memset(vis,0,sizeof(vis));        queue<int>q;        q.push(s);        d[s]=0;        vis[s]=1;        while (!q.empty())        {            int x = q.front();q.pop();            for (int i = 0;i<G[x].size();i++)            {                Edge &e = edges[G[x][i]];                if (!vis[e.to] && e.cap > e.flow)                {                    vis[e.to]=1;                    d[e.to] = d[x]+1;                    q.push(e.to);                }            }        }        return vis[t];    }    int DFS(int x,int a)    {        if (x==t || a==0)            return a;        int flow = 0,f;        for(int &i=cur[x];i<G[x].size();i++)        {            Edge &e = edges[G[x][i]];            if (d[x]+1 == d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0)            {                e.flow+=f;                edges[G[x][i]^1].flow-=f;                flow+=f;                a-=f;                if (a==0)                    break;            }        }        return flow;    }    int Maxflow(int s,int t)    {        this->s=s;        this->t=t;        int flow = 0;        while (BFS())        {            memset(cur,0,sizeof(cur));            flow+=DFS(s,INF);        }        return flow;    }}DC;int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        for(int i = 0;i<=n;i++)            e[i].clear();        for(int i = 1;i<=m;i++)        {            int u,v,di;            scanf("%d%d%d",&u,&v,&di);            e[u].push_back(Node(v,di));            e[v].push_back(Node(u,di));        }        s=1,t=n;        spfa();        DC.init();        for(int i = 1;i<=n;i++)//在跑过最短路的图上建立新图            for(int j = 0;j<e[i].size();j++)                if(d[e[i][j].v]==d[i]+1)                    DC.AddEdge(i,e[i][j].v,e[i][j].w);        printf("%d\n",DC.Maxflow(s,t));//在新图上跑最大流    }    return 0;}
0 0
原创粉丝点击