传送门
题目大意:给出一个n个点m条边的无向图,每一条边有长度和代价,先求1-n的长度最短路,在求去掉最小代价的边,使1-n的长度最短路变大
首先建出来最短路径图,然后连边容量为代价,跑最小割就行了
最短路径图也就是图上的每一条边都在至少一条最短路中,判断的时候只需要判断边(u,v,c)是否满足dis(u)+c=dis(v)就行了
让这些最短路都不能1和n连通所以跑一下最小割就行了
#include
#include
#include
#include
#include
#include
using namespace std;
#define N 505
#define E 300000
#define inf 1000000000
int n,m,maxflow,x[E],y[E],f[E],g[E];
int tot,point[N],nxt[E],v[E],c[E],remain[E];
int dis[N],vis[N],deep[N],last[N],cur[N],num[N];
queue <int> q;
void add(int x,int y,int z)
{
++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; c[tot]=z;
}
void spfa()
{
memset(dis,127/3,sizeof(dis));
dis[1]=0;vis[1]=1;q.push(1);
while (!q.empty())
{
int now=q.front();q.pop();
vis[now]=0;
for (int i=point[now];i;i=nxt[i])
if (dis[v[i]]>dis[now]+c[i])
{
dis[v[i]]=dis[now]+c[i];
if (!vis[v[i]]) vis[v[i]]=1,q.push(v[i]);
}
}
}
void addedge(int x,int y,int cap)
{
// printf("%d %d %d\n",x,y,cap);
++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap;
++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;
}
void bfs(int t)
{
for (int i=1;i<=t;++i) deep[i]=t,cur[i]=point[i];
deep[t]=0;q.push(t);
while (!q.empty())
{
int now=q.front();q.pop();
for (int i=point[now];i!=-1;i=nxt[i])
if (deep[v[i]]==t&&remain[i^1])
{
deep[v[i]]=deep[now]+1;
q.push(v[i]);
}
}
}
int addflow(int s,int t)
{
int now=t,ans=inf;
while (now!=s)
{
ans=min(ans,remain[last[now]]);
now=v[last[now]^1];
}
now=t;
while (now!=s)
{
remain[last[now]]-=ans;
remain[last[now]^1]+=ans;
now=v[last[now]^1];
}
return ans;
}
void isap(int s,int t)
{
bfs(t);
for (int i=1;i<=t;++i) ++num[deep[i]];
int now=s;
while (deep[s]if (now==t)
{
maxflow+=addflow(s,t);
now=s;
}
bool has_find=0;
for (int i=cur[now];i!=-1;i=nxt[i])
{
cur[now]=i;
if (deep[v[i]]+1==deep[now]&&remain[i])
{
has_find=1;
last[v[i]]=i;
now=v[i];
break;
}
}
if (!has_find)
{
int minn=t-1;
for (int i=point[now];i!=-1;i=nxt[i])
if (remain[i]) minn=min(minn,deep[v[i]]);
if (!(--num[deep[now]])) break;
++num[deep[now]=minn+1];
cur[now]=point[now];
if (now!=s) now=v[last[now]^1];
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=m;++i)
{
scanf("%d%d%d%d",&x[i],&y[i],&f[i],&g[i]);
add(x[i],y[i],f[i]),add(y[i],x[i],f[i]);
}
spfa();
printf("%d\n",dis[n]);
tot=-1;memset(point,-1,sizeof(point));
for (int i=1;i<=m;++i)
{
if (dis[x[i]]==dis[y[i]]+f[i]) addedge(y[i],x[i],g[i]);
if (dis[y[i]]==dis[x[i]]+f[i]) addedge(x[i],y[i],g[i]);
}
isap(1,n);
printf("%d\n",maxflow);
}