洛谷P1186玛丽卡
传送门啦
先跑一遍最短路,将最短路的路径记录下来,然后枚举每一条最短路的边,将其断掉,记录此时的1-n的时间,取其中最大的一个时间即为所求。
(通过 $ cut[][] $ 和 $ f[] $ 进行操作)
注意这个题是个稠密图,可能会卡 $ spfa $ ,所以我用了堆优化 $ dijk $ 。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1005;
const int maxm = 500005;
inline int read(){char ch = getchar();int f = 1 , x = 0;while(ch > '9' || ch < '0'){if(ch == '-')f = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + ch - '0';ch = getchar();}return x * f;}
int n,m,u,v,w;
int head[maxn],tot;
int dis[maxn],f[maxn],ans;
bool flag,cut[maxn][maxn];
struct Edge{
int from,to,next,val;
}edge[maxm << 1];
struct Node{
int u,d;
bool operator < (const Node &f) const {
return d > f.d;
}
} ;
void add(int u,int v,int w){
edge[++tot].from = u;
edge[tot].to = v;
edge[tot].val = w;
edge[tot].next = head[u];
head[u] = tot;
}
void dijk(int s){
for(int i=1;i<=n;i++) dis[i] = 1e9;
priority_queue<Node> q;
dis[s] = 0;
q.push( (Node) {s , 0} );
while(!q.empty()){
Node cur = q.top();
q.pop();
int u = cur.u , d = cur.d;
if(d != dis[u]) continue;
for(int i=head[u];i;i=edge[i].next){
int v = edge[i].to;
if(dis[v] > dis[u] + edge[i].val && cut[u][v] == 0){
if(!flag) f[v] = u;
dis[v] = dis[u] + edge[i].val;
q.push((Node) {v , dis[v]});
}
}
}
}
int main(){
n = read(); m = read();
for(int i=1;i<=m;i++){
u = read(); v = read(); w = read();
add(u , v , w);
add(v , u , w);
}
dijk(1);
flag = 1;
for(int i=n;i!=1;i=f[i]){
cut[f[i]][i] = 1;
cut[i][f[i]] = 1;
dijk(1);
cut[f[i]][i] = 0;
cut[i][f[i]] = 0;
ans = max(ans , dis[n]);
}
printf("%d\n",ans);
return 0;
}
洛谷P1186玛丽卡的更多相关文章
- 洛谷P1186 玛丽卡 spfa+删边
洛谷P1186 玛丽卡http://blog.csdn.net/huihao123456/article/details/73414139题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. ...
- 洛谷——P1186 玛丽卡
P1186 玛丽卡 题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道 ...
- 洛谷 P1186 玛丽卡
P1186 玛丽卡 题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道 ...
- 洛谷—— P1186 玛丽卡
https://www.luogu.org/problem/show?pid=1186 题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长 ...
- 洛谷P1186 玛丽卡
题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城 ...
- P1186 玛丽卡
题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城 ...
- luogu P1186 玛丽卡
题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城 ...
- Luogu P1186 玛丽卡 【最短路】By cellur925
题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城 ...
- 【luogu P1186 玛丽卡】 题解
题目链接:https://www.luogu.org/problemnew/show/P1186 邻接表开大开小真的奇妙,毒瘤玩意,再您妈的见. #include <queue> #inc ...
随机推荐
- vue入门教程
vue视频教程(对vue有个概览,要掌握vue-cli的用法,对vue-router,vuex有基本的概念) https://www.imooc.com/learn/1091 1. vue-cli v ...
- 解题:USACO12OPEN Bookshelf
题面 从零开始的DP学习之肆 当DP方程中的一部分具有某种单调性时可以用数据结构或者预处理维护来降低复杂度 一开始没有看懂题,尴尬,后来发现题目可以简化成这个样子: 将一个序列划分为若干段,每段长度不 ...
- 布隆过滤器 Bloom Filter
使用普通集合来判断一个元素是否已存在于集合中,需要占用比较大的空间.而使用Bloom Filter 可有效节省空间. Bloom Filter 以较少的内存占用及较小的误判率达到判断元素是否存已经加入 ...
- Eloquent 条件查询——tucker-eric/eloquentfilter 笔记
请阅读 https://github.com/Tucker-Eric/EloquentFilter , 里面有很全的文档和注释,以下仅列出关键部分. 1. 安装 composer require tu ...
- 使用docker配置etcd集群
docker配置etcd集群与直接部署etcd集群在配置上并没有什么太大差别. 我这里直接使用docker-compose来实现容器化的etcd部署 环境如下: HostName IP etcd1 1 ...
- ElasticStack系列之十六 & ElasticSearch5.x index/create 和 update 源码分析
开篇 在ElasticSearch 系列十四中提到的问题即 ElasticStack系列之十四 & ElasticSearch5.x bulk update 中重复 id 性能骤降,继续这个问 ...
- SQL Server 2008定期的备份数据库--差异+完整
https://www.cnblogs.com/l1pe1/p/7885207.html https://www.cnblogs.com/tylerflyn/p/8051398.html https: ...
- layoutSubviews中判断横竖屏
在ContentView中重写layoutSubviews方法,然后根据stausbar的方向判断当前视图的横竖屏.具体代码: -(void)layoutSubviews{ [super layout ...
- 安装VisualSVN Server 报错The specified TCP port is occupied
安装过程中报错,如下图所示. The specified TCP port is occupied by another service.Please stop that service or use ...
- COGS 1516. 棋盘上的车
COGS 1516. 棋盘上的车 http://www.cogs.pro/cogs/problem/problem.php?pid=1516 ☆ 输入文件:rook.in 输出文件:rook. ...