首页 > 代码库 > BZOJ 1266 上学路线(最短路+最小割)

BZOJ 1266 上学路线(最短路+最小割)

给出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-9
# define MOD 1000000000
# define INF 1000000000
# 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<<1,l,mid
# define rch p<<1|1,mid+1,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=0,f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
    return x*f;
}
void Out(int a) {
    if(a<0) {putchar(-); a=-a;}
    if(a>=10) Out(a/10);
    putchar(a%10+0);
}
const int N=505;
//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=1, cnt1=2, n, m, s, t;
struct qnode{
    int v, c;
    qnode(int _v=0, int _c=0):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=0; edge1[cnt1].next=head1[v]; head1[v]=cnt1++;
}
void Dijkstra(int n, int start){
    mem(vis,0); FOR(i,1,n) dist[i]=INF;
    dist[start]=0; que.push(qnode(start,0));
    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,-1);
    vis[s]=0; Q.push(s);
    while (!Q.empty()) {
        v=Q.front(); Q.pop();
        for (i=head1[v]; i; i=edge1[i].next) {
            if (edge1[i].w>0 && vis[edge1[i].p]==-1) {
                vis[edge1[i].p]=vis[v] + 1;
                Q.push(edge1[i].p);
            }
        }
    }
    return vis[t]!=-1;
}
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>0 && vis[edge1[i].p]==vis[x]+1){
            a=dfs(edge1[i].p,min(edge1[i].w,temp));
            temp-=a; edge1[i].w-=a; edge1[i^1].w+=a;
            if (temp==0) break;
        }
    }
    if (temp==low) vis[x]=-1;
    return low-temp;
}
int main ()
{
    int u, v, d, w;
    scanf("%d%d",&n,&m);
    FOR(i,1,m) scanf("%d%d%d%d",&u,&v,&d,&w), add_edge(u,v,d,w);
    Dijkstra(n,1);
    printf("%d\n",dist[n]);
    mem(vis,0); 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=1; t=n;
    int tmp, sum=0;
    while (bfs()) while (tmp=dfs(s,INF)) sum+=tmp;
    printf("%d\n",sum);
    return 0;
}
View Code

 

BZOJ 1266 上学路线(最短路+最小割)