zoukankan      html  css  js  c++  java
  • Cyclic Tour HDUOJ 费用流

    Cyclic Tour

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
    Total Submission(s): 1399    Accepted Submission(s): 712


    Problem Description
    There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him?
     
    Input
    There are several test cases in the input. You should process to the end of file (EOF).
    The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).
     
    Output
    Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1.
     
    Sample Input
    6 9 1 2 5 2 3 5 3 1 10 3 4 12 4 1 8 4 6 11 5 4 7 5 6 9 6 5 4 6 5 1 2 1 2 3 1 3 4 1 4 5 1 5 6 1
     
    Sample Output
    42 -1
    Hint
    In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
     
    Author
    RoBa@TJU
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1533 3395 3315 1565 2448

    费用流,拆点,source连接每个入点,费用0,容量1,每个出点连sink,费用0,容量1,入点出点见连费用边权,容量1,裸的费用流。

    只要注意一下spfa的队列数组开大一点。

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    #define MAXN 210
    #define INF 0x3f3f3f3f
    //AC
    int n,m;
    struct Edge 
    {
            int np,val,c;
            Edge *next,*neg;
    }E[MAXN*MAXN],*V[MAXN*2];
    int tope,sour=0,sink=1;
    void add_edge(int x,int y,int z,int c)
    {
            //cout<<"Add"<<x<<" "<<y<<" "<<z<<" "<<c<<endl;
            E[++tope].np=y;
            E[tope].val=z;
            E[tope].c=c;
            E[tope].next=V[x];
            V[x]=&E[tope];
            E[++tope].np=x;
            E[tope].val=0;
            E[tope].c=-c;
            E[tope].next=V[y];
            V[y]=&E[tope];
    
            E[tope].neg=&E[tope-1];
            E[tope-1].neg=&E[tope];
    }
    int q[MAXN*10],vis[MAXN],dis[MAXN],dfn=0;
    int prev[MAXN];
    Edge *path[MAXN];
    int spfa()
    {
            int ope=-1,clo=0,now;
            Edge *ne;
            memset(dis,INF,sizeof(dis));
            dfn++;
            q[0]=sour;
            vis[sour]=dfn;
            dis[sour]=0;
            while (ope<clo)
            {
                    now=q[++ope];
                    vis[now]=0;
                    for (ne=V[now];ne;ne=ne->next)
                    {
                            if (ne->val&&dis[ne->np]>dis[now]+ne->c)
                            {
                                    dis[ne->np]=dis[now]+ne->c;
                                    prev[ne->np]=now;
                                    path[ne->np]=ne;
                                    if (vis[ne->np]!=dfn)
                                    {
                                            vis[ne->np]=dfn;
                                            q[++clo]=ne->np;
                                    }
                            }
                    }
            }
            return dis[sink];
    }
    pair<int,int> max_cost_flow()
    {
            int ds,fl,now,x;
            pair<int,int> ret;
            ret.first=ret.second=0;
            while (ds=spfa(),ds!=INF)
            {
                    x=sink;
                    fl=INF;
                    while (x!=sour)
                    {
                            fl=min(fl,path[x]->val);
                            x=prev[x];
                    }
                    x=sink;
                    while (x!=sour)
                    {
                            path[x]->val-=fl;
                            path[x]->neg->val+=fl;
                            x=prev[x];
                    }
                    ret.first+=fl;
                    ret.second+=ds*fl;
            }
            return ret;
    }
    int main()
    {
            freopen("input.txt","r",stdin);
            int i,j,k,x,y,z;
            while (~scanf("%d%d",&n,&m))
            {
                    tope=-1;
                    memset(V,0,sizeof(V));
                    for (i=0;i<n;i++)
                    {
                            add_edge(sour,2+i,1,0);
                            add_edge(2+i+n,sink,1,0);
                    }
                    for(i=0;i<m;i++)
                    {
                            scanf("%d%d%d",&x,&y,&z);x--;y--;
                            add_edge(2+x,2+y+n,1,z);
                    }
                    pair<int,int> p1;
                    p1=max_cost_flow();
                    if (p1.first!=n)
                    {
                            printf("-1
    ");
                    }else
                    {
                            printf("%d
    ",p1.second);
                    }
            }
    }
    by mhy12345(http://www.cnblogs.com/mhy12345/) 未经允许请勿转载

    本博客已停用,新博客地址:http://mhy12345.xyz

  • 相关阅读:
    [Aaronyang] 写给自己的WPF4.5 笔记23 [3d交互与动画 4/4]
    [Aaronyang] 写给自己的WPF4.5 笔记22 [3d交互与动画 3/4]
    [Aaronyang] 写给自己的WPF4.5 笔记21 [3d课 2/4]
    [Aaronyang] 写给自己的WPF4.5 笔记20 [3d课 1/4]
    [Aaronyang] 写给自己的WPF4.5 笔记19[Visual类图文并茂讲解]
    [Aaronyang] 写给自己的WPF4.5 笔记18[几何图形*Geometry图文并茂讲解]
    [Aaronyang紫色博客] 写给自己的WPF4.5-Blend5公开课系列 3
    [Aaronyang紫色博客] 写给自己的WPF4.5-Blend5公开课系列 2-更进一步
    [Aaronyang紫色博客] 写给自己的WPF4.5-Blend5公开课系列 1
    [Aaronyang] 写给自己的WPF4.5 笔记15[AyArc诞生-WPF版本绚丽的环状图,Ay制作,AyWindow强势预览]
  • 原文地址:https://www.cnblogs.com/mhy12345/p/3774524.html
Copyright © 2011-2022 走看看