CCF-CSP题解 201703-4 地铁修建
求1-n最长边最小的路径。
最短路变形。dis值向后延申的方式是:$$dis[j]=min(dis[j],max(dis[i],w(i,j))$$
显然满足dijkstra贪心的选择方式。spfa也当然可以用。
写上三种方式,就当是模板好了。
spfa
复杂度:\(O(kE)/O(VE)\)
spfa的主要思想是不断松弛。注意spfa的更新策略,先更新\(dis\)值,再根据\(vis\)判断是否丢到\(queue\)中。
#include <bits/stdc++.h>
const int maxn = 100000;
const int maxm = 200000;
using namespace std;
int n, m;
int to[maxm * 2 + 10];
int w[maxm * 2 + 10];
int nex[maxm * 2 + 10];
int head[maxn + 10], cnt = 0;
void addEdge(int a, int b, int c)
{
to[cnt] = b; w[cnt] = c;
nex[cnt] = head[a]; head[a] = cnt++;
to[cnt] = a; w[cnt] = c;
nex[cnt] = head[b]; head[b] = cnt++;
}
int vis[maxn + 10];
int dis[maxn + 10];
void spfa()
{
queue<int> q;
dis[1] = 0;
q.push(1);
vis[1] = 1;
while (!q.empty())
{
int x = q.front(); q.pop();
vis[x] = 0;
// printf("current node: %d %d\n", x, dis[x]);
for (int i = head[x]; i != -1; i = nex[i])
{
int l = to[i];
if (max(dis[x], w[i]) < dis[l])
{
dis[l] = max(dis[x], w[i]);
if (!vis[l])
{
q.push(l);
vis[l] = 1;
}
}
}
}
}
int main()
{
scanf("%d%d", &n, &m);
memset(head, -1, sizeof(head));
for (int i = 1, a, b, c; i <= m; i++)
{
scanf("%d%d%d", &a, &b, &c);
addEdge(a, b, c);
}
memset(vis, 0, sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
spfa();
printf("%d\n", dis[n]);
return 0;
}
dijkstra
会超时。
复杂度:\(O(V^2)\)
dijkstra的主要思想是一共\(V\)次贪心的选择当前未确定点中\(dis\)值最小的那一个确定。
#include <bits/stdc++.h>
const int inf = 0x3f3f3f3f;
const int maxn = 100000;
const int maxm = 200000;
using namespace std;
int n, m;
int to[maxm * 2 + 10];
int w[maxm * 2 + 10];
int nex[maxm * 2 + 10];
int head[maxn + 10], cnt = 0;
void addEdge(int a, int b, int c)
{
to[cnt] = b; w[cnt] = c;
nex[cnt] = head[a]; head[a] = cnt++;
to[cnt] = a; w[cnt] = c;
nex[cnt] = head[b]; head[b] = cnt++;
}
int done[maxn + 10];
int dis[maxn + 10];
void dijkstra()
{
dis[1] = 0;
for (int i = 1; i <= n; i++)
{
int x = 0, mmin = inf;
for (int j = 1; j <= n; j++)
{
if (!done[j] && dis[j] < mmin)
mmin = dis[x = j];
}
done[x] = 1;
for (int j = head[x]; j != -1; j = nex[j])
{
int l = to[j];
dis[l] = min(dis[l], max(dis[x], w[j]));
}
}
}
int main()
{
scanf("%d%d", &n, &m);
memset(head, -1, sizeof(head));
for (int i = 1, a, b, c; i <= m; i++)
{
scanf("%d%d%d", &a, &b, &c);
addEdge(a, b, c);
}
memset(done, 0, sizeof(done));
memset(dis, 0x3f, sizeof(dis));
dijkstra();
printf("%d\n", dis[n]);
return 0;
}
heap_dijkstra
复杂度(stl优先队列实现,由于每条边最多被访问一次,堆中最多会有\(E\)个节点):\(O(ElogE)\),当图趋于完全图时,复杂度趋于\(O(V^2logV)\),应当使用一般实现的dijkstra算法。
堆优化dijkstra,主要思想是利用堆加速每一次值最小(未确定)的点的选择。实际实现略有不同,所以复杂度并非\(O(VlogV)\)。
#include <bits/stdc++.h>
const int inf = 0x3f3f3f3f;
const int maxn = 100000;
const int maxm = 200000;
using namespace std;
int n, m;
int to[maxm * 2 + 10];
int w[maxm * 2 + 10];
int nex[maxm * 2 + 10];
int head[maxn + 10], cnt = 0;
void addEdge(int a, int b, int c)
{
to[cnt] = b; w[cnt] = c;
nex[cnt] = head[a]; head[a] = cnt++;
to[cnt] = a; w[cnt] = c;
nex[cnt] = head[b]; head[b] = cnt++;
}
struct tNode
{
int d, u; // estimated dis, id of vertex
tNode(int dd, int uu): d(dd), u(uu){}
bool operator < (const tNode &y) const
{
return d > y.d;
}
};
int done[maxn + 10];
int dis[maxn + 10];
void heap_dijkstra()
{
priority_queue<tNode> q;
dis[1] = 0;
q.push(tNode(0, 1));
while (!q.empty())
{
tNode x = q.top(); q.pop();
int u = x.u;
if (done[u])
continue;
done[u] = 1;
for (int i = head[u]; i != -1; i = nex[i])
{
int l = to[i];
if (dis[l] > max(dis[u], w[i]))
{
dis[l] = max(dis[u], w[i]);
q.push(tNode(dis[l], l));
}
}
}
}
int main()
{
scanf("%d%d", &n, &m);
memset(head, -1, sizeof(head));
for (int i = 1, a, b, c; i <= m; i++)
{
scanf("%d%d%d", &a, &b, &c);
addEdge(a, b, c);
}
memset(done, 0, sizeof(done));
memset(dis, 0x3f, sizeof(dis));
heap_dijkstra();
printf("%d\n", dis[n]);
return 0;
}
CCF-CSP题解 201703-4 地铁修建的更多相关文章
- CCF CSP 201703-4 地铁修建
博客中的文章均为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201703-4 地铁修建 问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市决定在1号到n ...
- CCF CSP 201703
CCF CSP 2017·03 做了一段时间的CCF CSP试题,个人感觉是这样分布的 A.B题基本纯暴力可满分 B题留心数据范围 C题是个大模拟,留心即可 D题更倾向于图论?(个人做到的D题基本都是 ...
- ccf 201703-4 地铁修建(95)(并查集)
ccf 201703-4 地铁修建(95) 使用并查集,将路径按照耗时升序排列,依次加入路径,直到1和n连通,这时加入的最后一条路径,就是所需要修建的时间最长的路径. #include<iost ...
- CSP 201703-4 地铁修建 最小生成树+并查集
地铁修建 试题编号: 201703-4 试题名称: 地铁修建 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力, ...
- CSP 201703-4 地铁修建【最小生成树+并查集】
问题描述 试题编号: 201703-4 试题名称: 地铁修建 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市 ...
- CCF(地铁修建):向前星+dijikstra+求a到b所有路径中最长边中的最小值
地铁修建 201703-4 这题就是最短路的一种变形,不是求两点之间的最短路,而是求所有路径中的最长边的最小值. 这里还是使用d数组,但是定义不同了,这里的d[i]就是表示从起点到i的路径中最长边中的 ...
- CCF CSP 201412-4 最优灌溉
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201412-4 最优灌溉 问题描述 雷雷承包了很多片麦田,为了灌溉这些麦田,雷雷在第一个麦田挖 ...
- CCF CSP 201703-3 Markdown
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201703-3 Markdown 问题描述 Markdown 是一种很流行的轻量级标记语言(l ...
- CCF CSP 201312-3 最大的矩形
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201312-3 最大的矩形 问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i( ...
- CCF CSP 201609-3 炉石传说
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201609-3 炉石传说 问题描述 <炉石传说:魔兽英雄传>(Hearthston ...
随机推荐
- Blocked a frame with origin XXX from accessing a cross-origin 。iframe跨域问题
在前端开发的过程中,我们常常会用到iframe去在我们的页面中引用一个子页面,而父子页面又常常会有交互.在同域情况下,子页面如果想要访问父页面中的window对象中的方法的话,直接在当前页面中使用wi ...
- centos 生成网卡UUID
在Linux或CentOS中,可以通过如下命令获取网卡的uuid信息: uuidgen 网卡名07d07031-eb0f-4691-8606-befb46645433 查看网卡UUID nmcli c ...
- ftp用户和密码
centos7 FTP修改密码: 1.查看ftp的用户:cat /etc/vsftpd/ftpusers 2.passwd ftp的用户 (输入两次) 3.重启ftp:service vsftpd r ...
- Obtaining the backtrace - libunwind
Sometimes when working on a large project, I find it useful to figure out all the places from which ...
- Java入门系列之集合HashMap源码分析(十四)
前言 我们知道在Java 8中对于HashMap引入了红黑树从而提高操作性能,由于在上一节我们已经通过图解方式分析了红黑树原理,所以在接下来我们将更多精力投入到解析原理而不是算法本身,HashMap在 ...
- 《浅入浅出》-RocketMQ
你知道的越多,你不知道的越多 点赞再看,养成习惯 本文GitHub https://github.com/JavaFamily 已收录,有一线大厂面试点脑图.个人联系方式和技术交流群,欢迎Star和指 ...
- 金蝶天燕中间拒绝put、delete请求解决方案
项目要求支持国产化,那就国产化呗!使用金蝶天燕中间件替代weblogic,一切部署好后发现所有以put.delete请求的按钮全部无效,原因是中间件配置文件默认拒绝put.delete请求 解决方案为 ...
- String s = "a";与String s = new String("a")的区别
String s1 = "a" 时,首先会在字符串常量池中查找有无 “a” 这个对象. 若没找到,就创建一个 "a" 对象, 然后,以 s1 为它的引用.若在字 ...
- SpringBoot 整合mongoDB并自定义连接池
SpringBoot 整合mongoDB并自定义连接池 得力于SpringBoot的特性,整合mongoDB是很容易的,我们整合mongoDB的目的就是想用它给我们提供的mongoTemplate,它 ...
- KETTLE单表同步,写入EXCEL和TXT
以下操作都在5.0.1版本下进行开发,其余版本可以进行自动比对 在平时工作当中,会遇到这种情况,而且很常见.比如:1.自动生成文件TXT或者EXCEL(电信行业该需求居多),上传至某服务器:2.双方数 ...