Poj2135FarmTour [maximum minimum cost of undirected graphs]

Source: Internet
Author: User
Question: poj2135FarmTour. Analysis: If this question is not carefully read, it may be used as the shortest path. the shortest path is not necessarily the best. it is the shortest path of the two, but not necessarily the shortest path. We can use the billing flow to easily solve the problem. The Edge building capacity is 1, the cost is the edge right, and then the source node s connects to 1, and the fee is

Title: poj 2135 Farm Tour


Question: Give an undirected graph, ask from to, and then return to a total of the shortest paths.


Analysis: If this question is not carefully read, it may be used as the shortest path. the shortest path is not necessarily the best. it is the shortest path of the two, but not necessarily the shortest path.

We can easily solve the problem by using the billing flow. The Edge building capacity is 1, the cost is the edge right, and then the source node s connects to 1, the cost is 0, the capacity is 2, and the n point connects to the sink, CAPACITY 2, cost 0, you can.

Note that this topic is an undirected graph, so we need to create a bidirectional edge.


AC code:

#include 
 
  #include #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include 
       
        using namespace std;const int N = 1050;const int inf = 0x3f3f3f3f;#define Del(a,b) memset(a,b,sizeof(a))struct Node{ int from,to,cap,flow,cost;};vector
        
          v[N];vector
         
           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
          
            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
           
            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)){} return cost;}void Clear(int x){ for(int i=0; i<=x; i++) v[i].clear(); e.clear();}int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { for(int i=0;i
            
             

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.