您的位置:首页 > 其它

BZOJ1266 [AHOI2006] 上学路线route

2015-12-19 14:29 369 查看
题目链接: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之和
先跑一遍最短路,然后将最短路上的边跑一遍最小割就得到答案

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define rep(i,l,r) for(int i=l; i<=r; i++)
#define clr(x,y) memset(x,y,sizeof(x))
#define travel(x) for(Edge *p=last[x]; p; p=p->pre)
typedef long long ll;
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 510;
struct Edge{
Edge *pre,*rev; int to,cost;
}edge[250010];
Edge *last[maxn],*cur[maxn],*pt;
int n,m,ans=0,a[124760],b[124760],c[124760],t[124760],d[2][maxn];
bool isin[maxn];
queue <int> q;
inline int read(){
int ans = 0, f = 1;
char c = getchar();
while (!isdigit(c)){
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)){
ans = ans * 10 + c - '0';
c = getchar();
}
return ans * f;
}
inline void addedge(int x,int y,ll z){
pt->pre = last[x]; pt->to = y; pt->cost = z; last[x] = pt++;
}
inline void add(int x,int y,ll z){
addedge(x,y,z); addedge(y,x,0); last[x]->rev = last[y]; last[y]->rev = last[x];
}
void spfa(int x,int t){
clr(d[t],INF); d[t][x] = 0;
clr(isin,0); isin[x] = 1; q.push(x);
while (!q.empty()){
int now = q.front(); q.pop(); isin[now] = 0;
travel(now){
if (d[t][p->to] > d[t][now] + p->cost){
d[t][p->to] = d[t][now] + p->cost;
if (!isin[p->to]){
isin[p->to] = 1;
q.push(p->to);
}
}
}
}
}
bool bfs(){
while (!q.empty()) q.pop();
clr(d[0],-1); d[0][1] = 0; q.push(1);
while (!q.empty()){
int now = q.front(); q.pop();
travel(now){
if (d[0][p->to] == -1 && p->cost > 0){
d[0][p->to] = d[0][now] + 1;
q.push(p->to);
if (p->to == n) return 1;
}
}
}
return 0;
}
int dfs(int x,int flow){
if (x == n || (!flow)) return flow; int w = 0;
for(Edge *p=cur[x]; p && w < flow; p=p->pre){
if (d[0][p->to] == d[0][x] + 1 && p->cost > 0){
int delta = dfs(p->to,min(p->cost,flow-w));
p->cost -= delta;
p->rev->cost += delta;
w += delta;
if (p->cost) cur[x] = p;
}
}
if (w < flow) d[0][x] = -1;
return w;
}
int main(){
n = read(); m = read(); clr(last,0); pt = edge;
rep(i,1,m){
a[i] = read(); b[i] = read(); t[i] = read(); c[i] = read();
addedge(a[i],b[i],t[i]); addedge(b[i],a[i],t[i]);
}
spfa(1,0); spfa(n,1);
printf("%d\n",d[0]
);
clr(last,0); pt = edge;
rep(i,1,m){
if (d[0][a[i]] + t[i] + d[1][b[i]] == d[0]
){
add(a[i],b[i],c[i]);
}
if (d[0][b[i]] + t[i] + d[1][a[i]] == d[0]
){
add(b[i],a[i],c[i]);
}
}
while (bfs()){
rep(i,1,n) cur[i] = last[i];
ans += dfs(1,INF);
}
printf("%d\n",ans);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: