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 ...
随机推荐
- pop() 删除掉数组的最后一个元素
下面的代码首先创建了一个拥有四个元素的数组 myFish,然后删除掉它的最后一个元素. let myFish = ["angel", "clown", &quo ...
- 如何监控Redis性能指标(译)
Redis给人的印象是简单.很快,但是不代表它不需要关注它的性能指标,此文简单地介绍了一部分Redis性能指标.翻译过程中加入了自己延伸的一些疑问信息,仍然还有一些东西没有完全弄明白.原文中Metri ...
- appium python中的android uiautomator定位
text定位:driver.find_element_by_android_uiautomator('new UiSelector().text("请输入手机号")') #模糊定位 ...
- Task的在主线程处理异常信息的Helper类
最近使用task时候需要把异常记录日志,直接注入非单例模式的实例进入异步线程,在高并发情况下会出现一些问题. 所以需要把异常反馈给主线程 ,并且不在主线程里进行等待,研究相关资料后,自己写了一个简单的 ...
- Laravel 学习笔记
1. 简介 2. 运行环境要求 2.1 PHP版本 >= 5.5.9 2.2 Mcrypt PHP 扩展 --------------------------php的加密扩展,提供多种加密算法 ...
- 16. 3Sum Closest (JAVA)
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- 机器学习--k-means聚类原理
“物以类聚,人以群分”, 所谓聚类就是将相似的元素分到一"类"(有时也被称为"簇"或"集合"), 簇内元素相似程度高, 簇间元素相似程度低. ...
- pycrypto 安装 Crypto 报错 error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools&quo ...
- 浏览器标签栏logo添加
在<head > 中引入link,如下: <head> <link rel="icon" type="image/icon" hr ...
- ASP.NET Core使用NLog记录日志到Microsoft Sql Server
在之前的文章中介绍了如何在ASP.NET Core使用NLog,本文为您介绍在ASP.NET Core使用NLog记录到Microsoft Sql Server 1.我们需要添加依赖: NLog.We ...