1030 Travel Plan Dijkstra+dfs
#include <iostream>
#include <cstdio>
#include <vector>
#include<algorithm>
using namespace std;
int e[][], c[][];// distence cost
vector<int> pre[];//到达当前节点的前一个节点
vector<vector<int> > st; //最短距离的路径
vector<int> path, tmppath;
int inf = ;
int mincost = inf;
int totalcost = ;
int S;//由于dfs 要用到这个变量,所以从main里提前 出发城市
void dfs(int node, int front)
{
tmppath.push_back(node);
int xx;
if (front == -)
{
xx = ;
}
else
{
xx = c[node][front];
}
totalcost += xx;
if (node == S)
{
if (totalcost < mincost)
{
mincost = totalcost;
path.clear();
path = tmppath;
}
totalcost = totalcost - xx;
tmppath.pop_back();
return;
}
for (int i = ; i < pre[node].size(); i++)
{
dfs(pre[node][i], node);
}
totalcost = totalcost - xx;
tmppath.pop_back();
} int main()
{
int N, M, D;
int a, b, tmp1, tmp2;
fill(e[], e[] + * , inf);
fill(c[], c[] + * , inf);
scanf("%d%d%d%d", &N, &M, &S, &D);
for (int i = ; i < M; i++)
{
scanf("%d%d", &a, &b);
scanf("%d", &tmp1);
e[a][b] = e[b][a] = tmp1;
scanf("%d", &tmp2);
c[a][b] = c[b][a] = tmp2;
} //输入结束
//Dijkstra
int dis[];
int mark[];
fill(mark, mark + , );
fill(dis, dis + , inf);
dis[S] = ;
pre[S].push_back(S);
int point, near;//当前选择要加入的点 和 最近
for (int i = ; i < N; i++)
{
point = -, near = inf;
for (int j = ; j < N; j++)
{
if (mark[j] == && dis[j] < near)
{
point = j;
near = dis[j];
}
}
if (near == inf)break;//所有可达点都已加入
mark[point] = ;
for (int j = ; j < N; j++)
{
if (dis[j] > dis[point] + e[point][j])
{
dis[j] = dis[point] + e[point][j];
pre[j].clear();
pre[j].push_back(point);
}
else if (dis[j] == dis[point] + e[point][j])
{
pre[j].push_back(point);
}
} }
dfs(D, -);//pre存的是到当前节点的前一个节点,所以要倒回去
if(path.size()>)
for (int i = path.size() - ; i >= ; i--)
{
printf("%d ", path[i]);
}
else
printf("%d",S);
printf("%d %d", dis[D], mincost);
return ; }
1030 Travel Plan Dijkstra+dfs的更多相关文章
- 1030 Travel Plan (30 分)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, toge ...
- PAT1030 Travel Plan (30)---DFS
(一)题意 题目链接:https://www.patest.cn/contests/pat-a-practise/1030 1030. Travel Plan (30) A traveler's ma ...
- PAT 1030 Travel Plan[图论][难]
1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...
- PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, to ...
- [图算法] 1030. Travel Plan (30)
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
- 1030 Travel Plan (30 分)(最短路径 and dfs)
#include<bits/stdc++.h> using namespace std; ; const int inf=0x3f3f3f3f; int mp[N][N]; bool vi ...
- PAT Advanced 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]
题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...
- 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)
题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
- 1030 Travel Plan (30分)(dijkstra 具有多种决定因素)
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
随机推荐
- cuda 配置要点
1. 安装驱动 :sudo apt-get install nvidia- 2. 安装cuda : cuda 文件中包含驱动程序,因此在安装过程中当被问及是否安装驱动时,选择no 3. 安装cudnn ...
- yarn安装及node升级
ERROR: root@debian:/home/test/keygen-radio-master/scripts# npm install -g yarn npm WARN engine yarn@ ...
- java命令--jstat 工具使用
jstat(JVM Statistics Monitoring Tool)是用于监控虚拟机各种运行状态信息的命令行工具.他可以显示本地或远程虚拟机进程中的类装载.内存.垃圾收集.JIT编译等运行数据, ...
- Linux 用户与组
在 Linux 操作系统下,如何添加一个新用户到一个特定的组中?如何同时将用户添加到多个组中?又如何将一个已存在的用户移动到某个组或者给他增加一个组?对于不常用 Linux 的人来讲,记忆 Linux ...
- 让history显示时间
如何让history显示时间 linux和unix上都提供了history命令,可以查询以前执行的命令历史记录 但是,这个记录并不包含时间项目 因此只能看到命令,但是不知道什么时间执行的 如何让his ...
- kafka创建会话,报Error while executing topic command : Replication factor: 1 larger than available brokers: 0.
bin/kafka-topics.sh --create --zookeeper es1:2181 --replication-factor 1 --partitions 1 --topic top ...
- 【docker 入门 - 01】- Docker 在 Centos7 上安装与测试
一.学习文档 官网网站: https://www.docker.com 中文网站:http://www.docker-cn.com 官方安装文档:https://docs.docker.com/ins ...
- java 中拿项目路径
public class ItemPathInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHan ...
- CentOS7+CDH5.14.0安装CDH错误排查: HiveServer2 该角色的进程已退出。该角色的预期状态为已启动
错误提示: HiveServer2 该角色的进程已退出.该角色的预期状态为已启动 解决办法:出现此问题应该是内存不足造成的,重启相应的组件即可.比如Hive报错,重启Hive,YARN报错,重启YAR ...
- 正向与反向拓扑排序的区别(hdu 1285 确定比赛名次和hdu 4857 逃生)
确定比赛名次 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...