给出n个点的无向图,每条边有两个属性,边权和代价。
第一问求1-n的最短路。第二问求用最小的代价删边使得最短路的距离变大。
对于第二问。显然该删除的是出现在最短路径上的边。如果我们将图用最短路跑一遍预处理出所有最短路径。
然后我们要删除的边集一定是这个图的一个割。否则最短路径不会增加。即求此图的最小割。
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... struct Edge{int p, next, w, d;}edge[N*N];
struct Edge1{int p, next, w;}edge1[N*N];
int head[N], head1[N], dist[N], cnt=, cnt1=, n, m, s, t;
struct qnode{
int v, c;
qnode(int _v=, int _c=):v(_v),c(_c){}
bool operator<(const qnode &r)const{return c>r.c;}
};
int vis[N];
priority_queue<qnode>que;
queue<int>Q; void add_edge(int u, int v, int d, int w){
edge[cnt].p=v; edge[cnt].w=w; edge[cnt].d=d; edge[cnt].next=head[u]; head[u]=cnt++;
edge[cnt].p=u; edge[cnt].w=w; edge[cnt].d=d; edge[cnt].next=head[v]; head[v]=cnt++;
}
void add_edge1(int u, int v, int w){
edge1[cnt1].p=v; edge1[cnt1].w=w; edge1[cnt1].next=head1[u]; head1[u]=cnt1++;
edge1[cnt1].p=u; edge1[cnt1].w=; edge1[cnt1].next=head1[v]; head1[v]=cnt1++;
}
void Dijkstra(int n, int start){
mem(vis,); FOR(i,,n) dist[i]=INF;
dist[start]=; que.push(qnode(start,));
qnode tmp;
while (!que.empty()) {
tmp=que.top(); que.pop();
int u=tmp.v;
if (vis[u]) continue;
vis[u]=true;
for (int i=head[u]; i; i=edge[i].next) {
int v=edge[i].p, cost=edge[i].d;
if (!vis[v]&&dist[v]>dist[u]+cost) dist[v]=dist[u]+cost, que.push(qnode(v,dist[v]));
}
}
}
int bfs(){
int i, v;
mem(vis,-);
vis[s]=; Q.push(s);
while (!Q.empty()) {
v=Q.front(); Q.pop();
for (i=head1[v]; i; i=edge1[i].next) {
if (edge1[i].w> && vis[edge1[i].p]==-) {
vis[edge1[i].p]=vis[v] + ;
Q.push(edge1[i].p);
}
}
}
return vis[t]!=-;
}
int dfs(int x, int low){
int i, a, temp=low;
if (x==t) return low;
for (i=head1[x]; i; i=edge1[i].next) {
if (edge1[i].w> && vis[edge1[i].p]==vis[x]+){
a=dfs(edge1[i].p,min(edge1[i].w,temp));
temp-=a; edge1[i].w-=a; edge1[i^].w+=a;
if (temp==) break;
}
}
if (temp==low) vis[x]=-;
return low-temp;
}
int main ()
{
int u, v, d, w;
scanf("%d%d",&n,&m);
FOR(i,,m) scanf("%d%d%d%d",&u,&v,&d,&w), add_edge(u,v,d,w);
Dijkstra(n,);
printf("%d\n",dist[n]);
mem(vis,); Q.push(n); vis[n]=true;
while (!Q.empty()) {
int tmp=Q.front(); Q.pop();
for (int i=head[tmp]; i; i=edge[i].next) {
int v=edge[i].p;
if (dist[v]+edge[i].d!=dist[tmp]) continue;
add_edge1(v,tmp,edge[i].w);
if (!vis[v]) vis[v]=true, Q.push(v);
}
}
s=; t=n;
int tmp, sum=;
while (bfs()) while (tmp=dfs(s,INF)) sum+=tmp;
printf("%d\n",sum);
return ;
}