Hdoj 3488 tour [maximum Classic Minimum Cost stream]

Source: Internet
Author: User

Title: hdoj 3488 tour


Question: give n vertices and m edges, and then ask you to calculate that each vertex can only be in one ring (Hamilton ring), and the minimum cost of all vertices is only one time.


Analysis

Graph creation scheme:

Split each vertex into two I and II

Then s connects all I, capacity 1, cost 0

II connect all t, capacity 1, cost 0

Connect yii to all vertices on the right. The cost is the weight and the capacity is 0.


AC code:

#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;#define PB push_back#define MP make_pair#define Del(a,b) memset(a,b,sizeof(a))typedef vector<int> VI;typedef long long LL;const LL inf = 0x3f3f3f3f;const int N = 500;struct Node{    int from,to,cap,flow,cost;};vector<int> v[N];vector<Node> e;void add_Node(int from,int to,int cap,int cost){    e.push_back((Node)    {        from,to,cap,0,cost    });    e.push_back((Node)    {        to,from,0,0,-cost    });    int len = e.size()-1;    v[to].push_back(len);    v[from].push_back(len-1);}int vis[N],dis[N];int father[N],pos[N];bool BellManford(int s,int t,int& flow,int& cost){    Del(dis,inf);    Del(vis,0);    queue<int> q;    q.push(s);    vis[s]=1;    father[s]=-1;    dis[s] = 0;    pos[s] = inf;    while(!q.empty())    {        int f = q.front();        q.pop();        vis[f] = 0;        for(int i=0; i<v[f].size(); i++)        {            Node& tmp = e[v[f][i]];            if(tmp.cap>tmp.flow && dis[tmp.to] > dis[f] + tmp.cost)            {                dis[tmp.to] = dis[f] + tmp.cost;                father[tmp.to] = v[f][i];                pos[tmp.to] = min(pos[f],tmp.cap - tmp.flow);                if(vis[tmp.to] == 0)                {                    vis[tmp.to]=1;                    q.push(tmp.to);                }            }        }    }    if(dis[t] == inf)        return false;    flow += pos[t];    cost += dis[t]*pos[t];    for(int u = t; u!=s ; u = e[father[u]].from)    {        e[father[u]].flow += pos[t];        e[father[u]^1].flow -= pos[t];    }    return true;}int Mincost(int s,int t){    int flow = 0, cost = 0;    while(BellManford(s,t,flow,cost))    {        //printf("%d\n",cost);    }    return cost;}void Clear(int x){    for(int i=0; i<=x; i++)        v[i].clear();    e.clear();}char mp[120][120];int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n,m;        scanf("%d%d",&n,&m);        int s = 0 , t = 1;        for(int i=1;i<=n;i++)        {            add_Node(s,i*2,1,0);            add_Node(i*2+1,t,1,0);        }        for(int i=0;i<m;i++)        {            int x,y,z;            scanf("%d%d%d",&x,&y,&z);            add_Node(x*2,y*2+1,1,z);        }        int ans = Mincost(s,t);        printf("%d\n",ans);        Clear(2*n+5);    }    return 0;}


Hdoj 3488 tour [maximum Classic Minimum Cost stream]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.