1. 程式人生 > >網路流 dicnic sap 2種演算法詳細解釋

網路流 dicnic sap 2種演算法詳細解釋

Secret Milking Machine
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8189 Accepted: 2485

Description

Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within his farm and needs to be able to get to the machine without being detected. He must make a total of T (1 <= T <= 200) trips to the machine during its construction. He has a secret tunnel that he uses only for the return trips.

The farm comprises N (2 <= N <= 200) landmarks (numbered 1..N) connected by P (1 <= P <= 40,000) bidirectional trails (numbered 1..P) and with a positive length that does not exceed 1,000,000. Multiple trails might join a pair of landmarks.

To minimize his chances of detection, FJ knows he cannot use any trail on the farm more than once and that he should try to use the shortest trails.

Help FJ get from the barn (landmark 1) to the secret milking machine (landmark N) a total of T times. Find the minimum possible length of the longest single trail that he will have to use, subject to the constraint that he use no trail more than once. (Note well: The goal is to minimize the length of the longest trail, not the sum of the trail lengths.)

It is guaranteed that FJ can make all T trips without reusing a trail.

Input

* Line 1: Three space-separated integers: N, P, and T

* Lines 2..P+1: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, indicating that a trail connects landmark A_i to landmark B_i with length L_i.

Output

* Line 1: A single integer that is the minimum possible length of the longest segment of Farmer John's route.

Sample Input

7 9 2
1 2 2
2 3 5
3 7 5
1 4 1
4 3 1
4 5 7
5 7 1
1 6 3
6 7 3

Sample Output

5

Hint

Farmer John can travel trails 1 - 2 - 3 - 7 and 1 - 6 - 7. None of the trails travelled exceeds 5 units in length. It is impossible for Farmer John to travel from 1 to 7 twice without using at least one trail of length 5.

Huge input data,scanf is recommended.

Source

USACO 2005 February Gold


題意:

題意:FJ有N塊地,這些地之間有P條雙向路,每條路的都有固定的長度l。現在要你找出從第1塊地到第n塊地的T條不同路徑,每條路徑上的路不能與先前的路徑重複,問這些路徑中的最長路的最小是多少。

思路:二分答案+網路流判定。
二分列舉最大邊權,重新建圖,只儲存權不超過最大邊權的邊。即如果邊的長度小於等於我們規定的最大邊權 則新增這條邊 權值為1, 否則標記為0  
然後在網路中起點終點間的容量是原圖中的路徑數,判斷最大流是否>=T
 

只需要對模板就行修改下 之後二分即可   只需要修改新增邊的地方  因為本題是雙向邊 所以新增的邊以及其反方向邊 都應該權值為cw


  1. #include <stdio.h>
  2. #include <string.h>
  3. #define VM 222
  4. #define EM 81111*2
  5. #define inf 0x3f3f3f3f
  6. struct Edge  
  7. {  
  8.     int frm,to,cap,next;  
  9. }edge[EM];  
  10. int head[VM],dep[VM],ep,n;     //dep為點的層次
  11. void addedge (int cu,int cv,int cw)  //第一條邊下標必須為偶數
  12. {  
  13.     edge[ep].frm = cu;  
  14.     edge[ep].to = cv;  
  15.     edge[ep].cap = cw;  
  16.     edge[ep].next = head[cu];  
  17.     head[cu] = ep;  
  18.     ep ++;  
  19.     edge[ep].frm = cv;  
  20.     edge[ep].to = cu;  
  21.     edge[ep].cap = cw;  
  22.     edge[ep].next = head[cv];  
  23.     head[cv] = ep;  
  24.     ep ++;  
  25. }  
  26. int BFS (int src,int des)     //求出層次圖
  27. {  
  28.     int que[VM],i,front = 0,rear = 0;  
  29.     memset (dep,-1,sizeof(dep));  
  30.     que[rear++] = src;  
  31.     dep[src] = 0;  
  32.     while (front != rear)  
  33.     {  
  34.         int u = que[front++];  
  35.         front = front%VM;  
  36.         for (i = head[u];i != -1;i = edge[i].next)  
  37.         {  
  38.             int v = edge[i].to;  
  39.             if (edge[i].cap > 0&&dep[v] == -1) //容量大於0&&未在dep中
  40.             {  
  41.                 dep[v] = dep[u] + 1;        //建立層次圖
  42.                 que[rear ++] = v;  
  43.                 rear = rear % VM;  
  44.                 if (v == des)  //找到匯點 返回
  45.                     return 1;  
  46.             }  
  47.         }  
  48.     }  
  49.     return 0;  
  50. }  
  51. int dinic (int src,int des)  
  52. {  
  53.     int i,res = 0,top;  
  54.     int stack[VM];    //stack為棧,儲存當前增廣路
  55.     int cur[VM];        //儲存當前點的後繼 跟head是一樣的
  56.     while (BFS(src,des))   //if BFS找到增廣路
  57.     {  
  58.         memcpy (cur,head,sizeof (head));  
  59.         int u = src;       //u為當前結點
  60.         top = 0;  
  61.         while (1)  
  62.         {  
  63.             if (u == des)     //增廣路已全部進棧
  64.             {  
  65.                 int min = inf,loc ;  
  66.                 for (i = 0;i < top;i ++)       //找最小的增廣跟並loc記錄其在stack中位置
  67.                     if (min > edge[stack[i]].cap)  //以便退回該邊繼續DFS
  68.                     {  
  69.                         min = edge[stack[i]].cap;  
  70.                         loc = i;  
  71.                     }  
  72.                 for (i = 0;i < top;i ++)   //偶數^1 相當加1 奇數^1相當減1 當正向邊 = 0&&路徑不合適時,正加負減
  73.                 {                           //偶數是正向邊,奇數是負向邊,邊從0開始
  74.                     edge[stack[i]].cap -= min;  
  75.                     edge[stack[i]^1].cap += min;  
  76.                 }                              //將增廣路中的所有邊修改
  77.                 res += min;  
  78.                 top = loc;  
  79.                 u = edge[stack[top]].frm;         //當前結點修改為最小邊的起點
  80.             }  
  81.             for (i = cur[u];i != -1;cur[u] = i = edge[i].next)   //找到當前結點對應的下一條邊
  82.                 if (edge[i].cap != 0&&dep[u] + 1 == dep[edge[i].to])//不滿足條件時,修改cur值(去掉不合適的佔)eg:1-->2 1-->3 1-->4 有邊 但只有
  83.                     break;                                  // 1-->4 這條邊滿足條件 就把1到2、3的邊給去掉
  84.             if (cur[u] != -1)            //當前結點的下一條邊存在
  85.             {  
  86.                 stack[top ++] = cur[u];   //把該邊放入棧中
  87.                 u = edge[cur[u]].to;         //再從下個點開始找
  88.             }  
  89.             else
  90.             {  
  91.                 if (top == 0)        //當前結點無未遍歷的下一條邊且棧空,DFS找不到下一條增廣路
  92.                     break;  
  93.                 dep[u] = -1;            //當前結點不在增廣路中,剔除該點
  94.                 u = edge[stack[--top]].frm; //退棧 回朔,繼續查詢
  95.             }  
  96.         }  
  97.     }  
  98.     return res;  
  99. }  
  100. struct IN  
  101. {  
  102.     int x,y,c;  
  103. }q[EM];  
  104. int sovle(int mid,int src,int des,int m,int k)  
  105. {  
  106.     ep = 0;  
  107.     memset (head,-1,sizeof(head));  
  108.     for(int i=0;i<m;i++)  
  109.     {  
  110.        if(q[i].c<=mid)  
  111. 相關推薦

    網路 dicnic sap 2演算法詳細解釋

    Secret Milking Machine Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8189 Accepted: 2485 Description Farmer Jo

    最大EK、Dinic、SAP演算法模板

    EK   //Max_flow //@2018/05/02 Wednesday //EK algorithm [Edmonds Karp] O(V*E^2) O(v^2) //by Tawn #include <bits/stdc++.h> using nam

    (教你徹底理解)網路:基本概念與演算法 最大最小割

    一.網路流:流&網路&割 1.網路流問題(NetWork Flow Problem): 給定指定的一個有向圖,其中有兩個特殊的點源S(Sources)和匯T(Sinks),每條邊有指定的容量(Capacity),求滿足條件的從S到T的最大流(Max

    最大網路基礎概念+三個演算法

    下面是由一道題引發的一系列故事。。。 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 68920 Accepted: 26683 Description E

    mycat高可用 ER分片的2場景詳細分析

    1,ER分片關係簡介 有一類業務,例如訂單(ORDER)跟訂單明細表(ORDER_DETAIL),明細表會依賴二訂單,就是該會存在表的主從關係,這類似業務的切分可以抽象出合適的切分規則,比如根據使用者ID切分,其它相關的表都依賴於使用者ID,再或者根據訂單ID

    網路 Dinic + SAP(模版)

    參考博文 參考博文 只列出幾種常見的,快的網路流演算法 1.Dinic Dinic利用dis陣列進行分層,每次流的時候都只流到下一層,大大減少時間複雜度 struct Edge{ int v, w, nxt; }; Edge e

    採用SVM和神經網路的車牌識別(流程圖及詳細解釋

    一、整個程式的流程圖: 二、車牌定位中分割流程圖: 關於程式碼兩個if(r<1)的詳解: 參考:RotatedRect和CvBox2D。CvBox2D結構如下:(重點是angle的註釋) 三、車牌識別中字元分割流程圖:

    容斥原理 —— 求1~n有多少個數與k互質(二進位制演算法詳細解釋&模板)

    這裡有一道經典的例題,可以看一下:點選開啟連結 這裡的n可能要大於k的,所以不能用尤拉函式去做。 我們首先把k分解質因數,儲存到p陣列中,num表示質因子的數量。 void pr(int k) //求k的質因子 { num = 0; for (int i = 2 ;

    網路最大sap()演算法

             現在想將一些物資從S運抵T,必須經過一些中轉站。連線中轉站的是公路,每條公路都有最大運載量。          每條弧代表一條公路,弧上的數表示該公路的最大運載量。最多能將多少貨物從S運抵T?                   這是一個典型的網路流模

    自動排班系統2.0(基於網路實現的排班系統,附詳細註解)

    更新說明:     將輸入優化了下,不必再輸入幹部數和總班數,比較排序部分採用了氣泡排序。 程式碼: #include <iostream> #include <cstdio> #include <queue> #include &l

    HDU 3549 Flow Problem 網路最大問題 EK、Dinic、ISAP三演算法

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 8218    Accepted

    網路 最大—最小割 之SAP演算法 詳解

    首先引入幾個新名詞: 1、距離標號: 所謂距離標號 ,就是某個點到匯點的最少的弧的數量(即邊權值為1時某個點到匯點的最短路徑長度)。 設點i的標號為level[i],那麼如果將滿足level[i]=level[j]+1的弧(i,j)叫做允許弧 ,且增廣時只走允許弧

    網路(Edmonds-karp演算法 來自汝佳大神)

    #include<iostream> #include<cstdio> using namespace std; struct edge{ int from,to,cap,flow; edge(int u,int v,int c,int f):from(

    【POJ - 2226】Muddy Fields(匈牙利演算法網路dinic,二分圖匹配,最小點覆蓋,矩陣中優秀的建圖方式 )

    題幹: Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <= 50). While good for the gra

    網路 - 最大演算法之EK

    首先是網路流中的一些定義: V表示整個圖中的所有結點的集合. E表示整個圖中所有邊的集合. G = (V,E) ,表示整個圖. s表示網路的源點,t表示網路的匯點. 對於每條邊(u,v),有一個容量c(u,v)   (c(u,v)>=0),如果c(u,v)=0,則表示(

    排序演算法之氣泡排序——2形式

    1.第一種形式氣泡排序 package Sort; public class BubbleSort1 {     public static void main(String[] args) {      &nbs

    HDU -3549 (最大網路——Edmonds_Karp演算法+Dinic演算法)

    HDU -3549 (最大網路流) Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the m

    7. 網路演算法--Ford-Fulkerson方法及其多種實現

    public  class Network {      //     私有成員變數     // 頂點連結串列陣列,陣列的每個元素對應於     // 與頂點相連的所有頂點形成的連結串列     

    線性規劃網路 :工廠最大效益——單純形演算法【超詳+題解】

    問題:某食品加工廠一共有三個車間,第一車間用 1 個單位的原料 N 可以加工 5 個單位的產品 A 或 2 個單位的產品 B。產品 A 如果直接售出,售價為 10 元,如果在第二車間繼續加工,則需要額外加工費 5 元,加工後售價為 19 元。產品 B 如果直接售出,售價 16

    網路之最大(Dinic演算法

    程式碼對應於 POJ - 3281 #include <iostream> #include <cstring> #include <cstdio> #include <queue> #define fuck