BZOJ_1266_[AHOI2006] school route route_minimum cut

BZOJ_1266_[AHOI2006] school route route_minimum cut

Description

Coco and Kaka live in the eastern suburbs of Hefei City, and they have to change trains several times every day to reach the school at the western end of the city. It wasn't until one day the two of them participated in the school's Informatics Olympiad team that they realized that the daily bus route to school wasn't necessarily optimal. Coco: "It's very likely that we waste a lot of time on the way to school. Let's write a program to calculate the minimum time needed to go to school!" There are N bus stops in Hefei, so let's number them as 1... Natural number of N, and think that Coco and Kaka's family live near the No. 1 bus station, and their school is at the No. N bus station. There are M direct bus routes in the city. The bus that executes the i-th route travels between stations pi and qi, and the time it takes from the starting point to the ending point is ti. (1<=i<=M, 1<=pi, qi<=N) Two people sit in front of the computer and quickly calculate the optimal ride plan based on the above information. However, Coco suddenly had a crazy idea. He wanted to take advantage of Kaka's unpreparedness and delete some routes in Kaka's input data, so that the answer obtained by Kaka's program was greater than the actual shortest time. For each route i, there is actually a cost ci: the larger the ci of the deleted route, the easier it is for Kaka to find this joke. Coco wants to know what kind of deletion scheme can achieve his purpose and let the deleted bus The sum of the routes ci is the smallest. [Task] Write a program that:  Read the information of the bus routes in Hefei from the input file;  Calculate the minimum time it actually takes for Coco and Kaka to go to school;  Help Coco to design a plan to delete some of the input information For bus routes, the minimum time required to get from home to school after deletion becomes larger, and the ci sum of the deleted route is the smallest; output the answer to the output file.

Input

In the first line of the input file, there are two positive integers N and M, which represent the number of bus stops and bus routes in Hefei City, respectively. The following M lines, each line (the ith line, the total (i+1) line) uses four positive integers to describe the ith route: pi, qi, ti, ci; see the description above for the specific meaning.

Output

The output file has a maximum of two lines. There is only one integer in the first line, which represents the shortest time it takes to get from Coco and Kaka's home to school. The second line outputs an integer C, representing the sum of Ci

Sample Input

6 7
1 2 1 3
2 6 1 5
1 3 1 1
3 4 1 1
4 6 1 1
5 6 1 2
1 5 1 4

Sample Output

2
5

HINT

2<=N<=500, 1<=M<=124 750, 1<=ti, ci<=10 000
The bus network in Hefei is very developed, you can think that any two stations can be connected to each other by direct or transfer. Reach, of course, if in the deletion scheme you provided, home and school cannot reach each other, then the shortest required to go to school is considered to be positive infinity: this is obviously a legitimate scheme.


 Only the edges on the shortest path in the original graph are kept, so that as long as 1 and n are not connected, the shortest path becomes longer.

So the minimum cut can be found.

 

Code:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
using namespace __gnu_pbds;
#define N 550
#define M 300050
#define inf 100000000
int head[N],to[M],nxt[M],dis[N][2],val[M],flow[M],cnt,n,m,xx[M],yy[M],zz[M],cc[M];
int Q[N],l,r,dep[N],vis[N];
__gnu_pbds::priority_queue<pair<int,int> >q;
inline void add(int u,int v,int w) {
    to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt; val[cnt]=w;
}
inline void insert(int u,int v,int f) {
    to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt; flow[cnt]=f;
    to[++cnt]=u; nxt[cnt]=head[v]; head[v]=cnt; flow[cnt]=0;
}
void dij() {
    memset(dis,0x3f,sizeof(dis));
    dis[1][0]=0; q.push(make_pair(0,1));
    int i;
    while(!q.empty()) {
        int x=q.top().second; q.pop();
        if(vis[x]) continue;
        screw[x]=1;
        for(i=head[x];i;i=nxt[i]) {
            if(dis[to[i]][0]>dis[x][0]+val[i]) {
                dis[to[i]][0]=dis[x][0]+val[i];
                q.push(make_pair(-dis[to[i]][0],to[i]));
            }
        }
    }
    memset(vis,0,sizeof(vis)); dis[n][1]=0; q.push(make_pair(0,n));
    while(!q.empty()) {
        int x=q.top().second; q.pop();
        if(vis[x]) continue;
        screw[x]=1;
        for(i=head[x];i;i=nxt[i]) {
            if(dis[to[i]][1]>dis[x][1]+val[i]) {
                dis[to[i]][1]=dis[x][1]+val[i];
                q.push(make_pair(-dis[to[i]][1],to[i]));
            }
        }
    }
}
bool bfs() {
    memset(dep,0,sizeof(dep));
    l=r=0;Q[r++]=1;dep[1]=1;
    while(l<r) {
        int x=Q[l++],i;
        for(i=head[x];i;i=nxt[i]) {
            if(!dep[to[i]]&&flow[i]) {
                dep[to[i]]=dep[x]+1;
                if(to[i]==n) return 1;
                Q[r++]=to[i];
            }
        }
    }
    return 0;
}
int dfs(int x,int mf) {
    if(x==n) return mf;
    int nf=0,i;
    for(i=head[x];i;i=nxt[i]) {
        if(dep[to[i]]==dep[x]+1&&flow[i]) {
            int tmp=dfs(to[i],min(mf-nf,flow[i]));
            if(!tmp) dep[to[i]]=0;
            nf+=tmp;
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            if(nf==mf) break;
        }
    }
    return nf;
}
void dinic() {
    int ans=0,f;
    while(bfs()) while(f=dfs(1,inf)) ans+=f;
    printf("%d\n",ans);
}
int main() {
    scanf("%d%d",&n,&m);
    int i;
    for(i=1;i<=m;i++) {
        scanf("%d%d%d%d",&xx[i],&yy[i],&zz[i],&cc[i]);
        add(xx[i],yy[i],zz[i]);
        add(yy[i],xx[i],zz[i]);
    }
    dij ();
    memset(head,0,sizeof(head)); cnt=1;
    int len ​​= dis [n] [0];
    printf("%d\n",len);
    for(i=1;i<=m;i++) {
        if(dis[xx[i]][0]+dis[yy[i]][1]+zz[i]==len) {
            insert(xx[i],yy[i],cc[i]);
        }
        if(dis[yy[i]][0]+dis[xx[i]][1]+zz[i]==len) {
            insert(yy[i],xx[i],cc[i]);
        }
    }
    dinic();
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325025435&siteId=291194637