給定圖,求從 s 到 t 的最短路徑集合。需刪除一些邊使所有最短路被切斷,刪除代價為邊權 c,求最小總代價。等價於:在最短路子圖中求 s-t 最小割 = 最大流。
dis[u] = 最短距離dis[u] + w == dis[v]gap、dis、cur 當前弧優化// Dijkstra + 反向 BFS 找最短路邊
// 建流圖:G.add(u, v, c) // 容量為 c
// SAP 最大流
int sap(int s, int t, int n) { ... } // 標準 ISAP 實作
struct Graph { // 原圖
struct Edge { int u,v,w,c,next; } e[MAXM];
int head[MAXN];
void add(int u,int v,int w,int c) { ... }
} gs;
struct MaxFlow { // 流網絡
struct Edge { int u,v,w,next; } e[MAXM];
int head[MAXN], dis[MAXN], cur[MAXN], gap[MAXN], pre[MAXN], aug[MAXN];
void add(int u,int v,int w) { ... } // 残量網絡雙向邊
int sap(int s,int t,int n) { ... } // ISAP 主流程
} G;
int main() {
while (scanf("%d%d",&n,&m), n||m) {
gs.init(); scanf("%d%d",&st,&ed);
for (i=1;i<=m;i++) { scanf("%d%d%d%d",&a,&b,&w,&c); gs.add(a,b,w,c); gs.add(b,a,w,c); }
G.init(MAXN-1);
printf("%d\n", dijkstra_sap(st,ed,n));
}
}