显然,要删掉的路径肯定在最短路径上面。所以在最短路上建网络流的图,此时边的容量变成了ci。我们要花最小的代价让1,n不连通,也就是求最大流。
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define MAXN 510
using namespace std;
inline int Min(int a,int b)
{return a<b?a:b;}
inline void Swap(int &a,int &b)
{int c = a; a = b,b = c;}
struct E
{
int v,w,op;
E(){}
E(int a,int b,int c)
{v = a; w= b; op = c;}
};
vector<E> g[MAXN];
void add(int u,int v,int w)
{
g[u].push_back(E(v,w,g[v].size()));
g[v].push_back(E(u,0,g[u].size()-1));
}
int d[MAXN],vd[MAXN],s,t,n,m,flow;
int d1[MAXN],d2[MAXN];
struct Edge
{
int u,v,w,c;
}e[300010];
struct Node
{
int v,w,next;
}edge[300010];
int head[MAXN],cnt;
void Add_edge(int u,int v,int w)
{
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
}
bool inque[MAXN];
void SPFA(int s,int *dis)
{
queue<int> Q;
for(int i = 1; i <= n; i++)
dis[i] = 0x7f3f3f3f,inque[i] = 0;
dis[s] = 0,inque[s] = 1,Q.push(s);
while(!Q.empty())
{
int u = Q.front();
Q.pop(),inque[u] = 0;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
if(dis[u]+edge[i].w < dis[v])
{
dis[v] = dis[u]+edge[i].w;
if(!inque[v]) Q.push(v),inque[v] = 1;
}
}
}
}
int aug(int i,int augco)
{
int j,augc = augco,delta,mind = n-1,sz = g[i].size();
if(i == n) return augco;
for(j = 0; j < sz; j++)
{
int v = g[i][j].v;
if(g[i][j].w)
{
if(d[i] == d[v]+1)
{
delta = Min(augc,g[i][j].w);
delta = aug(v,delta);
g[i][j].w -= delta;
g[v][g[i][j].op].w += delta;
augc -= delta;
if(d[1] >= n) return augco - augc;
if(!augc) break;
}
if(d[v] < mind) mind = d[v];
}
}
if(augc == augco)
{
vd[d[i]]--;
if(!vd[d[i]]) d[1] = n;
d[i] = mind+1;
vd[d[i]]++;
}
return augco - augc;
}
void sap()
{
vd[0] = n;
while(d[1] < n)
flow += aug(1,0x7f3f3f3f);
}
int main()
{
memset(head,-1,sizeof head);
scanf("%d%d",&n,&m);
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d%d",&e[i].u,&e[i].v,&e[i].w,&e[i].c);
Add_edge(e[i].u,e[i].v,e[i].w);
Add_edge(e[i].v,e[i].u,e[i].w);
}
SPFA(1,d1);
SPFA(n,d2);
for(int i = 1; i <= m; i++)
{
int u = e[i].u,v = e[i].v;
if(d1[u] > d1[v]) Swap(u,v);
if(d1[u] + e[i].w + d2[v] == d1[n])
add(u,v,e[i].c);
}
sap();
printf("%d\n%d\n",d1[n],flow);
}