CCCC L2-001 紧急救援 floyd改的dijkstra模板 (记录路径) L3 天梯地图
https://www.patest.cn/contests/gplt/L2-001
题解:求最短路的条数,并输出点的权值最大的路径,用priority_queue会wa两个点,原因不明。
于是又学了另外一种dijkstra
坑:L3的天梯地图是同一个模板的题目,结果代码交上去有一个点过不了。。。。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <string.h>
#include <string>
#include <map>
#include<stack>
#include<set>
#include<string.h>
#define pb push_back
#define _for(i, a, b) for (int i = (a); i<(b); ++i)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i) using namespace std;
const int N = + ; int num[N];
int E[N][N];
int dis[N];
vector<int> path;
int ans[N];
int sum[N];
int last[N];
void init() {
_for(i, , N) {
dis[i] = 1e9;
//ans[i] = 1; }
}
int main() {
init();
memset(E, -, sizeof(E));
int n;
int m, s, d;
cin >> n >> m >> s >> d;
set<int>vis;
_for(i, , n) {
cin >> num[i]; sum[i] = num[i]; vis.insert(i);
}
_for(i, , m) {
int x, y, z;
cin >> x >> y >> z;
E[x][y] = E[y][x] = z;
} last[s] = -; ans[s] = ; dis[s] = ;
for (int now = s; vis.size()>; vis.erase(now)) {
int mn = 1e9;
_for(i, , n) {
if (dis[i] < mn&&vis.count(i) == ) {
now = i;
mn = dis[i];
}
}
_for(j, , N) if(E[now][j]!=-){ if (dis[j] > dis[now] + E[now][j]) {
dis[j] = dis[now] + E[now][j];
last[j] = now;
sum[j] = sum[now] + num[j];
ans[j] = ans[now];
}
else if (dis[j] == dis[now] + E[now][j]) {
ans[j] += ans[now];
if (sum[j] < sum[now] + num[j]) {
last[j] = now;
sum[j] = sum[now] + num[j];
}
}
}
} cout << ans[d] << ' ' << sum[d] << endl;
stack<int>st;
for (int i = d; i != -; i = last[i]) {
st.push(i);
} cout << st.top(); st.pop();
while (!st.empty()) { cout << ' ' << st.top(); st.pop(); } system("pause"); }
附上wa2的代码,以后研究(求助)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <string.h>
#include <string>
#include <map>
#include<stack>
#define pb push_back
#define _for(i, a, b) for (int i = (a); i<(b); ++i)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i) using namespace std;
const int N = +; int num[N];
vector< pair<int, int> > E[N];
int dis[N];
vector<int> path[N];
int ans[N];
int sum[N];
int last[N]; void init() {
_for(i, , N) {
dis[i] = 1e9;
ans[i] = ;
//last[i] = -1;
}
}
int main() {
init();
int n;
int m, s, d;
cin >> n >> m >> s >> d;
_for(i, , n) { cin >> num[i]; sum[i] = num[i]; }
_for(i, , m) {
int x, y, z;
cin >> x >> y >> z;
E[x].pb(make_pair(y, z));
E[y].pb(make_pair(x, z)); }
priority_queue<pair<int, int> >Q;
dis[s] = ;
path[s].pb(s);
Q.push(make_pair(-dis[s], s)); last[s] = -;
while (!Q.empty()) {
int now = Q.top().second;
Q.pop();
_for(i, , E[now].size()) {
int v = E[now][i].first;//about update the node,consider every step we enumerate all the nodes linked to the priority node ,if it is better put it into a vector temporarily.
if (dis[v] > dis[now] + E[now][i].second) {
dis[v] = dis[now] + E[now][i].second;
last[v] = now;
sum[v] = sum[now] + num[v];
ans[v] = ans[now];
Q.push(make_pair(-dis[v], v));
//here is the problem ,when we update dis,it's easy,but about path,we need to change the before ones
}
else if (dis[v] == dis[now] + E[now][i].second) {
ans[v] += ans[now];
if (sum[v]<sum[now]+num[v]) {
last[v] = now;
sum[v] = sum[now] + num[v];
Q.push(make_pair(-dis[v], v)
} }
}
}
cout << ans[d] << ' ' << sum[d] << endl;
stack<int> st;
for (int i = d; i != -; i = last[i]) {
st.push(i);
}
cout << st.top(); st.pop();
while (!st.empty()) { cout <<' '<< st.top(); st.pop(); }
system("pause"); }
L3的天梯地图:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <string.h>
#include <string>
#include <map>
#include<stack>
#include<set>
#include<string.h>
#define pb push_back
#define _for(i, a, b) for (int i = (a); i<(b); ++i)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i) using namespace std;
const int N = + ; int num[N];
int E[N][N],R[N][N];
int dis[N];
int di[N];
vector<int> path;
int ans[N];
int sum[N];
int last[N];
void init() {
_for(i, , N) {
dis[i] = 1e9;
//ans[i] = 1;
di[i] = 1e9;
}
}
int main() {
init();
memset(E, -, sizeof(E));
int n;
int m, s, d;
cin >> n >> m;
set<int>vis;
_for(i, , n) {
sum[i] = ; vis.insert(i); num[i] = ;
}
_for(i, , m) {
int x, y, z;
cin >> x >> y >> z;
int len, tim;
cin >> len >> tim;
if (z == )
E[x][y] = E[y][x] = len;
else E[x][y] = len;
if (z == )
R[x][y] = R[y][x] = tim;
else R[x][y] = tim;
}
cin >> s >> d; _for(i, , n) {
sum[i] = ; vis.insert(i); num[i] = ;
}
last[s] = -; ans[s] = ; di[s] = ;
for (int now = s; vis.size()>; vis.erase(now)) {
int mn = 1e9;
_for(i, , n) {
if (di[i] < mn&&vis.count(i) == ) {
now = i;
mn = di[i];
}
}
_for(j, , N) if (E[now][j] != -) { if (di[j] > di[now] + R[now][j]) {
di[j] = di[now] + R[now][j];
last[j] = now;
sum[j] = sum[now] + num[j];
ans[j] = ans[now];
}
else if (di[j] == di[now] + R[now][j]) {
ans[j] += ans[now];
if (sum[j] > sum[now] + num[j]) {
last[j] = now;
sum[j] = sum[now] + num[j];
}
}
}
} stack<int> st1;
for (int i = d; i != -; i = last[i]) {
st1.push(i);
} //printf("Time = %d: ", di[d]);
//cout << st.top(); st.pop();
//while (!st.empty()) { cout << " => " << st.top(); st.pop(); }
//cout << endl; _for(i, , n) {
sum[i] = ; vis.insert(i); num[i] = ;
}
last[s] = -; ans[s] = ; dis[s] = ;
for (int now = s; vis.size()>; vis.erase(now)) {
int mn = 1e9;
_for(i, , n) {
if (dis[i] < mn&&vis.count(i) == ) {
now = i;
mn = dis[i];
}
}
_for(j, , N) if (E[now][j] != -) { if (dis[j] > dis[now] + E[now][j]) {
dis[j] = dis[now] + E[now][j];
last[j] = now;
sum[j] = sum[now] + num[j];
ans[j] = ans[now];
}
else if (dis[j] == dis[now] + E[now][j]) {
ans[j] += ans[now];
if (sum[j] > sum[now] + num[j]) {
last[j] = now;
sum[j] = sum[now] + num[j];
}
}
}
} stack<int> st2;
for (int i = d; i != -; i = last[i]) {
st2.push(i);
}
if (st1 == st2) {
printf("Time = %d; Distance = %d: ", di[d],dis[d]);
cout << st2.top(); st2.pop();
while (!st2.empty()) { cout << " => " << st2.top(); st2.pop(); }
}
else {
printf("Time = %d: ", di[d]);
cout << st1.top(); st1.pop();
while (!st1.empty()) { cout << " => " << st1.top(); st1.pop(); }
cout << endl;
printf("Distance = %d: ", dis[d]);
cout << st2.top(); st2.pop();
while (!st2.empty()) { cout << " => " << st2.top(); st2.pop(); }
cout << endl;
}
system("pause"); }
CCCC L2-001 紧急救援 floyd改的dijkstra模板 (记录路径) L3 天梯地图的更多相关文章
- 天梯 L2 紧急救援 (dijkstra变形+记录路径)
L2-001 紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道 ...
- L2-001. 紧急救援---(Dijkstra,记录路径)
https://www.patest.cn/contests/gplt/L2-001 L2-001. 紧急救援 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 ...
- [Python] 弗洛伊德(Floyd)算法求图的直径并记录路径
相关概念 对于一个图G=(V, E),求图中两点u, v间最短路径长度,称为图的最短路径问题.最短路径中最长的称为图的直径. 其中,求图中确定的某两点的最短路径算法,称为单源最短路径算法.求图中任意两 ...
- HDU-2544 最短路 Dijkstra模板题
题目链接:https://vjudge.net/problem/HDU-2544 题意: 题目要求找到节点1到节点n之间的一条最短路 分析: Dijkstra模板题 单源最短路径,可以用dijkstr ...
- Minimum Transport Cost(floyd+二维数组记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)
layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...
- HDU-1595Find the longest of shortest(最短路径的最长路Dijkstra+记录路径)
Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she do ...
- HDU 2544 最短路(floyd+bellman-ford+spfa+dijkstra队列优化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目大意:找点1到点n的最短路(无向图) 练一下最短路... dijkstra+队列优化: #i ...
- L2-001. 紧急救援 (Dijkstra算法打印路径)
作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...
随机推荐
- Ubuntu 12.04下安装QQ 2012 Beta3(转)
Ubuntu 12.04下安装QQ 2012 Beta3 由于wine的发展非常迅速.现在网上的利用老版本的wine来安装QQ2012的教程已经有些过时了.实际上操作起来非常简单: 第一步:Ctr ...
- mysql 外键约束示例
-- 创建测试主表. ID 是主键.CREATE TABLE test_main ( id INT, value VARCHAR(10), PRIMARY KEY(id)); -- ...
- 使用pyinotify监控文件系统的变化
pyinotify依赖Linux内核inotify功能,它需要在2.6.13版本的内核的Linux系统上运行. 1. 安装pyinotify pip install pyinotify 安装完后可以直 ...
- using 释放内存的写法
using (FileStream fileStream = File.Open(fileName,FileMode.Open,FileAccess.Read,FileShare.ReadWrite) ...
- 第二十篇:不为客户连接创建子进程的并发回射服务器(poll实现)
前言 在上文中,我使用select函数实现了不为客户连接创建子进程的并发回射服务器( 点此进入 ).但其中有个细节确实有点麻烦,那就是还得设置一个client数组用来标记select监听描述符集中被设 ...
- Kafka producer拦截器(interceptor)
Producer拦截器(interceptor)是个相当新的功能,它和consumer端interceptor是在Kafka 0.10版本被引入的,主要用于实现clients端的定制化控制逻辑. 对于 ...
- 这样理解 HTTPS 更容易(Maybe)
摘要:本文尝试一步步还原HTTPS的设计过程,以理解为什么HTTPS最终会是这副模样.但是这并不代表HTTPS的真实设计过程.在阅读本文时,你可以尝试放下已有的对HTTPS的理解,这样更利于“还原”过 ...
- IOS 怎么用UIScrollView来滚动和缩放他的内容第一篇
本篇文章来自于互联网资料翻译 UIScrollView是在IOS最有用的控件之一.他是一个来展现超过一个屏幕的内容的很好的方式.下面有很多的技巧来使用他. 这篇文章就是关于UIScrollView的, ...
- 【cs229-Lecture12】K-means算法
上课内容: 无监督学习: K-means聚类算法 混合高斯模型 jensen不等式(用于推导出EM算法的一般形式) EM(Expectation Maximization)算法(最大期望算法) K-m ...
- QT开发之旅二TCP调试工具
TCP调试工具顾名思义用来调试TCP通信的,网上这样的工具N多,之前用.NET写过一个,无奈在XP下还要安装个.NET框架才能运行,索性这次用QT重写,发现QT写TCP通信比.NET还要便捷一些,运行 ...