和1018思路如出一辙,先求最短路径,再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的更多相关文章

  1. 1030 Travel Plan (30 分)

    1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, toge ...

  2. PAT1030 Travel Plan (30)---DFS

    (一)题意 题目链接:https://www.patest.cn/contests/pat-a-practise/1030 1030. Travel Plan (30) A traveler's ma ...

  3. PAT 1030 Travel Plan[图论][难]

    1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...

  4. 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 ...

  5. [图算法] 1030. Travel Plan (30)

    1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...

  6. 1030 Travel Plan (30 分)(最短路径 and dfs)

    #include<bits/stdc++.h> using namespace std; ; const int inf=0x3f3f3f3f; int mp[N][N]; bool vi ...

  7. 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 ...

  8. 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)

    题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...

  9. 1030 Travel Plan (30分)(dijkstra 具有多种决定因素)

    A traveler's map gives the distances between cities along the highways, together with the cost of ea ...

随机推荐

  1. document.compatMode 浏览器渲染模式判定利器

    在加了DOCTYPE的页面document.compatMode输出CSS1Compat,不管加的是XHTML的还是HTML5的DOCTYPE.没有加的输出BackCompat. BackCompat ...

  2. CentOS6.3上搭建expect无交互开发环境

    1.背景 在面向shell编程时对于需要交互的场合则必须通过人工来干预,而对于这种方式是违反无人职守的原则:现在expect就解决了这个问题, Expect是一个免费的编程工具语言,用来实现自动和交互 ...

  3. 2018/12/22:centos中转换目录时/root的影响

    今天在将一个压缩包复制到/root下,并解压.从表面看我试在根目录下,但是就是不能进入生成的目录,提示no such file or diraction.最后我加上/root又好了,奇怪 编译环境:输 ...

  4. php 安装最新的redis连接扩展

    用于与redis连接的Php扩展[RC表示公测,我们用的是不带RC的稳定版本]下载包地址:http://pecl.php.net/package/redis 最新稳定版本:4.3.0 下载包:# wg ...

  5. 初次见面,C++(的博客作业)

    懵懂无知.编程是什么?要是你在两个月前问我这个问题,你可能只会得到一个回答“天知道”.依稀记得在初中,上电脑课的老师曾经触及过编程的皮毛(那时候也没有编程的概念,听的似懂非懂),旁边一个同学却在十分熟 ...

  6. 牛客小白月赛13 小A的柱状图(单调栈)

    链接:https://ac.nowcoder.com/acm/contest/549/H来源:牛客网 题目描述 柱状图是有一些宽度相等的矩形下端对齐以后横向排列的图形,但是小A的柱状图却不是一个规范的 ...

  7. js 中格式化显示时间

    function getMyDateTime(str){ var oDate = new Date(str), oYear = oDate.getFullYear(), oMonth = oDate. ...

  8. springmvc接收ajax传递的数组

    之前的方法我用字符串拼接.req.getParameter("参数名[]");或json方式.虽然都能用,但是都不太令我满意. 今天参考这个贴子,ajax添加 traditiona ...

  9. webpack and publish lib

    http://keer2345.github.io/2018/04/13/webpack-4-tutorial-example-with-npm/ https://blog.csdn.net/feng ...

  10. 48-设置tomcat虚拟路径的两种方法(Eclipse、tomcat、IDEA)

    设置tomcat虚拟路径的两种方法(Eclipse.tomcat.IDEA) 三种方式设置虚拟服务器路径如果我们要实现一个上传文件的功能,但是又想要上传的文件不会随着自己web服务器的重启而不能访问了 ...