洛谷 P1186 玛丽卡
来源:互联网 发布:最早的程序员 编辑:程序博客网 时间:2024/05/16 17:31
题目描述
麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复。
因为她和他们不住在同一个城市,因此她开始准备她的长途旅行。
在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城市路上所需花费的时间。
麦克在车中无意中听到有一条路正在维修,并且那儿正堵车,但没听清楚到底是哪一条路。无论哪一条路正在维修,从玛丽卡所在的城市都能到达麦克所在的城市。
玛丽卡将只从不堵车的路上通过,并且她将按最短路线行车。麦克希望知道在最糟糕的情况下玛丽卡到达他所在的城市需要多长时间,这样他就能保证他的女朋友离开该城市足够远。
编写程序,帮助麦克找出玛丽卡按最短路线通过不堵车道路到达他所在城市所需的最长时间(用分钟表示)。
输入输出格式
输入格式:
第一行有两个用空格隔开的数N和M,分别表示城市的数量以及城市间道路的数量。1≤N≤1000,1≤M≤N*(N-1)/2。城市用数字1至N标识,麦克在城市1中,玛丽卡在城市N中。
接下来的M行中每行包含三个用空格隔开的数A,B和V。其中1≤A,B≤N,1≤V≤1000。这些数字表示在A和城市B中间有一条双行道,并且在V分钟内是就能通过。
输出格式:
输出文件的第一行中写出用分钟表示的最长时间,在这段时间中,无论哪条路在堵车,玛丽卡应该能够到达麦克处,如果少于这个时间的话,则必定存在一条路,该条路一旦堵车,玛丽卡就不能够赶到麦克处。
输入输出样例
输入样例#1:
5 7
1 2 8
1 4 10
2 3 9
2 4 10
2 5 1
3 4 7
3 5 10
输出样例#1:
27
【分析】
先跑一遍spfa判断标记最短路,然后枚举哪条最短路上的路径被删掉,再跑spfa,不断更新答案即可
【代码】
#include<iostream>#include<queue>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#define ll long long#define M(a) memset(a,0,sizeof a)#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;const int mxn=2000005;int n,m,cnt,ans;int head[1005],dis[1005],whe[mxn];bool vis[1005],yes[mxn];struct edge {int to,next,dis;} f[mxn];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(int u,int v,int d){ f[++cnt].to=v; f[cnt].dis=d; f[cnt].next=head[u]; head[u]=cnt;}inline void spfa(){ queue <int> q; memset(dis,0x3f,sizeof dis); q.push(1); dis[1]=0; while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u];i;i=f[i].next) { int v=f[i].to,d=f[i].dis; if(dis[v]>dis[u]+d) { dis[v]=dis[u]+d; whe[v]=u; if(!vis[v]) q.push(v),vis[v]=1; } } }}inline void spfa(int t1,int t2){ queue <int> q; memset(dis,0x3f,sizeof dis); q.push(1); dis[1]=0; while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u];i;i=f[i].next) { int v=f[i].to,d=f[i].dis; if(u==t1 && v==t2) continue; if(u==t2 && v==t1) continue; if(dis[v]>dis[u]+d) { dis[v]=dis[u]+d; if(!vis[v]) q.push(v),vis[v]=1; } } } ans=max(ans,dis[n]);}int main(){ int i,j,u,v,d; n=read(),m=read(); fo(i,1,m) { u=read(),v=read(),d=read(); add(u,v,d),add(v,u,d); } spfa(); u=n; while(u!=1) { spfa(u,whe[u]); u=whe[u]; } printf("%d\n",ans); return 0;}
- 洛谷 P1186 玛丽卡
- 洛谷 P1186 玛丽卡
- 洛谷 P1186 玛丽卡
- 洛谷 P1186 玛丽卡
- 洛谷 P1186 玛丽卡
- 玛丽卡 (codevs p1021;洛谷p1186)
- 【洛谷 P1186】玛丽卡 spfa+删边
- P1186 玛丽卡
- Codevs 1021 玛丽卡==洛谷 P1186
- 【题解】P1186玛丽卡(最短路+图论+SPFA+SLF优化)
- luogu解题报告:P1186玛丽卡【图论/最短路/堆优化dijkstra】
- 洛谷1186 玛丽卡
- 洛谷1186-玛丽卡
- 【洛谷 1186】 玛丽卡
- 洛谷1186 玛丽卡
- 玛丽卡
- 玛丽卡
- 玛丽卡
- Mark!Android最佳的开源库集锦
- (Java)LeetCode-72. Edit Distance
- git管理工具上传代码到github
- Drawing a Custom Mainframe Window for MFC SDI Applications
- 【mfc】_pFirstBlock==pHead解决办法
- 洛谷 P1186 玛丽卡
- poj 1743 Musical Theme(后缀数组+二分答案+height数组分组)
- CentOS 7 安装Memcached服务
- epub转换mobi
- Java 8 Lambda 学习笔记
- 【DP】【分队问题】
- noip2014联合权值 (贪心+数学推导)
- 转载博文:Centos 下打造合适的科研环境
- 【NOIP 模拟题】[T2]分解数(线性筛+贪心)