bzoj1266【AHOI2006】上学路线route
来源:互联网 发布:cms建站系统 java 编辑:程序博客网 时间:2024/04/29 23:06
1266: [AHOI2006]上学路线route
Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 1602 Solved: 559
[Submit][Status][Discuss]
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之和
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
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
5
HINT
2<=N<=500, 1<=M<=124 750, 1<=ti, ci<=10 000
合肥市的公交网络十分发达,你可以认为任意两个车站间都可以通过直达或转车互相到达,当然如果在你提供的删除方案中,家和学校无法互相到达,那么则认为上学需要的最短为正无穷大:这显然是一个合法的方案。
Source
先对原图求一遍最短路,然后把可能在最短路上的边加入到新图中,在新图中求最小割。
那么怎样判断一条边是否可能在最短路上呢?设某条边为(x,y,v),那么如果d[x][0]+d[y][1]+v=d[n][0],这条边就可能在最短路上(其中d[i][0]表示起点到i的最短路,d[i][1]表示终点到i的最短路)。
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<queue>#define F(i,j,n) for(int i=j;i<=n;i++)#define D(i,j,n) for(int i=j;i>=n;i--)#define ll long long#define pa pair<int,int>#define maxn 550#define maxm 300000#define inf 1000000000using namespace std;struct edge_type{int to,next,v;}e[maxm];struct edge_2{int to,next,v,c;}ea[maxm];int head[maxn],cur[maxn],dis[maxn],heada[maxn],d[maxn][2];int n,m,s,t,cnt=1,ans=0,cnta=0,p,q,v,c;bool inq[maxn];inline int read(){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;}inline void add_edge(int x,int y,int v){e[++cnt]=(edge_type){y,head[x],v};head[x]=cnt;e[++cnt]=(edge_type){x,head[y],0};head[y]=cnt;}inline void add_edgea(int x,int y,int v,int c){ea[++cnta]=(edge_2){y,heada[x],v,c};heada[x]=cnta;ea[++cnta]=(edge_2){x,heada[y],v,c};heada[y]=cnta;}inline bool bfs(){queue<int>q;memset(dis,-1,sizeof(dis));dis[s]=0;q.push(s);while (!q.empty()){int tmp=q.front();q.pop();if (tmp==t) return true;for(int i=head[tmp];i;i=e[i].next) if (e[i].v&&dis[e[i].to]==-1){dis[e[i].to]=dis[tmp]+1;q.push(e[i].to);}}return false;}inline int dfs(int x,int f){int tmp,sum=0;if (x==t) return f;for(int &i=cur[x];i;i=e[i].next){int y=e[i].to;if (e[i].v&&dis[y]==dis[x]+1){tmp=dfs(y,min(f-sum,e[i].v));e[i].v-=tmp;e[i^1].v+=tmp;sum+=tmp;if (sum==f) return sum;}}if (!sum) dis[x]=-1;return sum;}inline void dinic(){ans=0;while (bfs()){F(i,s,t) cur[i]=head[i];ans+=dfs(s,inf);}return;}inline void spfa(int s,int f){queue<int>q;memset(inq,false,sizeof(inq));d[s][f]=0;inq[s]=true;q.push(s);while (!q.empty()){int x=q.front();inq[x]=false;q.pop();for(int i=heada[x];i;i=ea[i].next){int y=ea[i].to;if (d[y][f]==-1||d[y][f]>d[x][f]+ea[i].v){d[y][f]=d[x][f]+ea[i].v;if (!inq[y]){inq[y]=true;q.push(y);}}}}}int main(){n=read();m=read();s=1;t=n;F(i,1,m){p=read();q=read();v=read();c=read();add_edgea(p,q,v,c);}memset(d,-1,sizeof(d));spfa(1,0);spfa(n,1);printf("%d\n",d[n][0]);F(i,1,n) for(int j=heada[i];j;j=ea[j].next){int y=ea[j].to;if (d[i][0]+ea[j].v+d[y][1]==d[t][0]) add_edge(i,y,ea[j].c);}dinic();printf("%d\n",ans);}
0 0
- bzoj1266【AHOI2006】上学路线route
- bzoj1266: [AHOI2006]上学路线route
- BZOJ1266: [AHOI2006]上学路线route
- Bzoj1266 Ahoi2006 上学路线
- 【BZOJ1266】【AHOI2006】上学路线route 最短路建图转最小割
- 【bzoj1266】[AHOI2006]上学路线route 最小割+floyd
- 【BZOJ1266】[AHOI2006]上学路线route【最短路图】【最小割】
- [BZOJ1266][AHOI2006]上学路线route(spfa+最小割)
- [BZOJ1266]-[AHOI2006]上学路线route-SPFA+最小割
- [AHOI2006] BZOJ1266 上学路线route-图论-最短路树-最小割
- 1266: [AHOI2006]上学路线route
- 1266: [AHOI2006]上学路线route
- 1266: [AHOI2006]上学路线route
- BZOJ1266 上学路线route(最小割)
- [BZOJ1266][AHOI2006][最短路][最小割]上学路线
- 【bzoj1266】【AHOI2006】【上学路线】【最短路+最小割】
- [bzoj1266][AHOI2006] 上学路线 最小割+最短路
- bzoj 1266 [AHOI2006] 上学路线 route 题解
- 版本控制器GIT、SVN初接触
- 03_单行函数 oracle学习笔记
- more effective c++(临时对象的返回值优化)
- leetcode -- Count Complete Tree Nodes -- 重点。BFS
- 明天决战英语四级
- bzoj1266【AHOI2006】上学路线route
- 生态系统评估-数据库设计
- HDOJ--2032
- 拥抱Android Studio:从Eclipse到Android Studio的完美迁徙
- GROUP BY 子句
- windows下打包cocos2d-x之自动复制第三方so文件
- 引子
- view和layer的区别
- HDOJ--2033