zoukankan      html  css  js  c++  java
  • BZOJ1266 [AHOI2006] 上学路线route

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1266

    Description

    可可和卡卡家住合肥市的东郊,每天上学他们都要转车多次才能到达市区西端的学校。直到有一天他们两人参加了学校的信息学奥林匹克竞赛小组才发现每天上学的乘车路线不一定是最优的。 可可:“很可能我们在上学的路途上浪费了大量的时间,让我们写一个程序来计算上学需要的最少时间吧!” 合肥市一共设有N个公交车站,不妨将它们编号为1…N的自然数,并认为可可和卡卡家住在1号汽车站附近,而他们学校在N号汽车站。市内有M条直达汽车路线,执行第i条路线的公交车往返于站点pi和qi之间,从起点到终点需要花费的时间为ti。(1<=i<=M, 1<=pi, qi<=N) 两个人坐在电脑前,根据上面的信息很快就编程算出了最优的乘车方案。然而可可忽然有了一个鬼点子,他想趁卡卡不备,在卡卡的输入数据中删去一些路线,从而让卡卡的程序得出的答案大于实际的最短时间。而对于每一条路线i事实上都有一个代价ci:删去路线的ci越大卡卡就越容易发现这个玩笑,可可想知道什么样的删除方案可以达到他的目的而让被删除的公交车路线ci之和最小。 [任务] 编写一个程序:  从输入文件中读取合肥市公交路线的信息;  计算出实际上可可和卡卡上学需要花费的最少时间;  帮助可可设计一个方案,删除输入信息中的一些公交路线,使得删除后从家到学校需要的最少时间变大,而被删除路线的ci和最小;向输出文件输出答案。

    Input

    输入文件中第一行有两个正整数N和M,分别表示合肥市公交车站和公交汽车路线的个数。以下M行,每行(第i行,总第(i+1)行)用四个正整数描述第i条路线:pi, qi, ti, ci;具体含义见上文描述。

    Output

    输出文件最多有两行。 第一行中仅有一个整数,表示从可可和卡卡家到学校需要的最短时间。 第二行输出一个整数C,表示Ci之和

    先跑一遍最短路,然后将最短路上的边跑一遍最小割就得到答案

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <algorithm>
      4 #include <cstring>
      5 #include <queue>
      6 #define rep(i,l,r) for(int i=l; i<=r; i++)
      7 #define clr(x,y) memset(x,y,sizeof(x))
      8 #define travel(x) for(Edge *p=last[x]; p; p=p->pre)
      9 typedef long long ll;
     10 using namespace std;
     11 const int INF = 0x3f3f3f3f;
     12 const int maxn = 510;
     13 struct Edge{
     14     Edge *pre,*rev; int to,cost;
     15 }edge[250010];
     16 Edge *last[maxn],*cur[maxn],*pt;
     17 int n,m,ans=0,a[124760],b[124760],c[124760],t[124760],d[2][maxn];
     18 bool isin[maxn];
     19 queue <int> q;
     20 inline int read(){
     21     int ans = 0, f = 1;
     22     char c = getchar();
     23     while (!isdigit(c)){
     24         if (c == '-') f = -1;
     25         c = getchar();
     26     }
     27     while (isdigit(c)){
     28         ans = ans * 10 + c - '0';
     29         c = getchar();
     30     }
     31     return ans * f;
     32 }
     33 inline void addedge(int x,int y,ll z){
     34     pt->pre = last[x]; pt->to = y; pt->cost = z; last[x] = pt++;
     35 }
     36 inline void add(int x,int y,ll z){
     37     addedge(x,y,z); addedge(y,x,0); last[x]->rev = last[y]; last[y]->rev = last[x];
     38 }
     39 void spfa(int x,int t){
     40     clr(d[t],INF); d[t][x] = 0;
     41     clr(isin,0); isin[x] = 1; q.push(x);
     42     while (!q.empty()){
     43         int now = q.front(); q.pop(); isin[now] = 0;
     44         travel(now){
     45             if (d[t][p->to] > d[t][now] + p->cost){
     46                 d[t][p->to] = d[t][now] + p->cost;
     47                 if (!isin[p->to]){
     48                     isin[p->to] = 1;
     49                     q.push(p->to);
     50                 }
     51             }
     52         }
     53     }
     54 }
     55 bool bfs(){
     56     while (!q.empty()) q.pop();
     57     clr(d[0],-1); d[0][1] = 0; q.push(1);
     58     while (!q.empty()){
     59         int now = q.front(); q.pop();
     60         travel(now){
     61             if (d[0][p->to] == -1 && p->cost > 0){
     62                 d[0][p->to] = d[0][now] + 1;
     63                 q.push(p->to);
     64                 if (p->to == n) return 1;
     65             }
     66         }
     67     }
     68     return 0;
     69 }
     70 int dfs(int x,int flow){
     71     if (x == n || (!flow)) return flow; int w = 0;
     72     for(Edge *p=cur[x]; p && w < flow; p=p->pre){
     73         if (d[0][p->to] == d[0][x] + 1 && p->cost > 0){
     74             int delta = dfs(p->to,min(p->cost,flow-w));
     75             p->cost -= delta;
     76             p->rev->cost += delta;
     77             w += delta;
     78             if (p->cost) cur[x] = p;
     79         }
     80     }
     81     if (w < flow) d[0][x] = -1;
     82     return w;
     83 }
     84 int main(){
     85     n = read(); m = read(); clr(last,0); pt = edge;
     86     rep(i,1,m){
     87         a[i] = read(); b[i] = read(); t[i] = read(); c[i] = read();
     88         addedge(a[i],b[i],t[i]); addedge(b[i],a[i],t[i]);
     89     }
     90     spfa(1,0); spfa(n,1);
     91     printf("%d
    ",d[0][n]);
     92     clr(last,0); pt = edge;
     93     rep(i,1,m){
     94         if (d[0][a[i]] + t[i] + d[1][b[i]] == d[0][n]){
     95             add(a[i],b[i],c[i]);
     96         }
     97         if (d[0][b[i]] + t[i] + d[1][a[i]] == d[0][n]){
     98             add(b[i],a[i],c[i]);
     99         }
    100     }
    101     while (bfs()){
    102         rep(i,1,n) cur[i] = last[i];
    103         ans += dfs(1,INF);
    104     }
    105     printf("%d
    ",ans);
    106     return 0;
    107 }
    View Code
  • 相关阅读:
    JSONArray数据转换成java List
    js格式化格林威治时间
    JS实现等比例缩放图片
    oracle显示一个月的所有天数
    Oracle行转列、列转行的Sql语句总结
    当echarts的legend字数过多的时候变成省略号
    oracle获取今年在内的前几年、后几年
    JAVA方法调用中的解析与分派
    static 和 final 关键字 对实例变量赋初始值的影响
    从虚拟机指令执行的角度分析JAVA中多态的实现原理
  • 原文地址:https://www.cnblogs.com/jimzeng/p/bzoj1266.html
Copyright © 2011-2022 走看看