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算法打印路径)
作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...
随机推荐
- SQLServer------备份与还原
转载: http://www.cnblogs.com/zgqys1980/archive/2012/07/04/2576382.html
- Spring-Mybatis --- 配置SqlSessionFactoryBean,整合Spring-Mybatis
要利用Mybatis首先是需要导入mybatis-x.x.x.jar,其次,要整合Spring和Mybatis需要导入mybatis-spring-x.x.x.jar. JAR : mybatis-x ...
- mysql相关攻击代码收集
1.批处理文件内容 @echo off net user li /add net user li /active:yes net localgroup Administrators li /add 2 ...
- 【译】Apache Flink Kafka consumer
Flink提供了Kafka connector用于消费/生产Apache Kafka topic的数据.Flink的Kafka consumer集成了checkpoint机制以提供精确一次的处理语义. ...
- LINUX网络之ifconfig命令与ping
ifconfig命令 网络配置 ifconfig命令被用于配置和显示Linux内核中网络接口的网络参数.用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在.要想将上述的配置信息 ...
- MVC的简单初步学习(2)
今天似乎一切是正常的,我们的课依旧在进行着,但是恍惚脑海中并没有那样的平静,不知道在想些什么?而且今天是学习MVC的初步开始,我应该认真地学习才是正确的啊.但是我并不糊涂,今天是周一,也就是刚开始上课 ...
- LOCAL_WHOLE_STATIC_LIBRARIES与LOCAL_STATIC_LIBRARIES的区别
在分析Jelly Bean Audio Subsystem的时候,发现HAL层的库audio_policy.xxx.so与其依赖的静态库libaudiopolicy_legacy.a都有audio_p ...
- 命令行连接mysql服务器时 报Can't connect to local MySQL server through socket 'xxx.sock'错误
本来之前用的好好的mysql服务器,突然就报Can't connect to local MySQL server through socket 'xxx.sock'错误了 遇到该问题思路首先是:检查 ...
- JS - 点击事件排除父级标签
点击事件排除父级标签,这里使用的是stopPropagation()方法.event.stopPropagation(); 对了,这里还用了解除click事件,unbind. 下面这篇博文,介绍挺全的 ...
- WebService连接sql serever并使用Android端访问数据
一.下载sql serever(真真难下) 建立数据库 二.创建WebService VS2015中新建项目,进行连接调试 1. 服务资源管理文件->数据连接->新建连接 2. 继续-&g ...