PAT A 1030. Travel Plan (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1030
找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时。
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<iostream>
#include<queue>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long LL;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int maxn = 5e2 + 10;
int n, m, s, t, map[maxn][maxn], cost[maxn][maxn], x, y, z, c;
int dis[maxn], v[maxn]; void dfs(int x)
{
if (x == t)return;
for (int i = 0; i < n; i++)
{
if (map[x][i])
{
if (dis[i]>dis[x]+map[x][i])
{
dis[i] = dis[x] + map[x][i];
v[i] = v[x] + cost[x][i];
dfs(i);
}
else if (dis[i] == dis[x] + map[x][i] && v[i] > v[x] + cost[x][i])
{
v[i] = v[x] + cost[x][i];
dfs(i);
}
}
}
} bool Dfs(int x)
{
if (x == s){ printf("%d ", s); return true; }
for (int i = 0; i < n; i++)
{
if (map[x][i] && dis[x] == dis[i] + map[x][i] && v[x] == v[i] + cost[x][i])
{
if (Dfs(i)){printf("%d ", x); return true;}
}
}
return false;
} int main()
{
scanf("%d%d%d%d", &n, &m, &s, &t);
while (m--)
{
scanf("%d%d%d%d", &x, &y, &z, &c);
if (!map[x][y] || map[x][y] > z)
{
map[x][y] = map[y][x] = z;
cost[x][y] = cost[y][x] = c;
}
else if (map[x][y] == z) cost[x][y] = cost[y][x] = min(z, cost[x][y]);
}
for (int i = 0; i < n; i++)dis[i] = v[i] = INF;
dis[s] = v[s] = 0;
dfs(s);
Dfs(t);
printf("%d %d\n", dis[t], v[t]);
return 0;
}
PAT A 1030. Travel Plan (30)【最短路径】的更多相关文章
- 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 ...
- 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 ...
- [图算法] 1030. Travel Plan (30)
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
- PAT (Advanced Level) 1030. Travel Plan (30)
先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath& ...
- PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径
模板题最短路+输出路径如果最短路不唯一,输出cost最小的 #include <iostream> #include <cstdio> #include <algorit ...
- 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)
题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
- 1030. Travel Plan (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...
- PAT 甲级 1030 Travel Plan
https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392 A traveler's map gives ...
- 1030 Travel Plan (30)(30 分)
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
随机推荐
- HttpResponse的使用方法
HttpResponse的使用方法: HttpRequest类是一个封闭HTTP提交信息的类型,而封闭HTTP输出信息的类型就是HttpResponse类,使用HttpResponse类可以实现三种类 ...
- ubuntu 下简单录音
找了半天录音工具,甚至都在尝试用 pyAudio 自己写了,结果发现,原来有现成命令行工具用! 就是 sox 工具包.这个工具包有 4 个工具:sox, play, rec, soxi.rec 和 p ...
- sqlalchemy入门记录
前言 发现翻译全文时间比较久,所以先整个简单的使用说明吧 安装SQLAlchemy pip install sqlalchemy 查看SQLAlchemy版本 In [1]: import sqlal ...
- Windows10环境搭建Elasticsearch+Kibana+Marvel
环境: Windows10企业版X64 Elasticsearch-2.4.1 Kibana-4.6.1 Marvel-2.0+ 步骤: 安装Elasticsearch:官网下载Elasticsear ...
- Java程序员
从生存.制胜.发展三个方面入手,为大家展示出程序员求职与工作的一幅3D全景图像.本书中既有在公司中的生存技巧,又有高手达人的进阶策略,既有求职攻略的按图索骥,又有入职后生产环境的破解揭秘. 书中浓缩了 ...
- Android系统学习小记
序言 Android 应用的启动到一个页面显示出来,这个过程涉及到点击事件的处理,以及如何启动一个Activity,启动一个Activity之后,如何将Activity中我们的设置的ContentVi ...
- flask初探
为什么我们需要模板 让我们来考虑下我们该如何扩充我们这个小的应用程序. 我们希望我们的微博应用程序的主页上有一个欢迎登录用户的标题,这是这种类型的应用程序的一个"标配".忽略本应用 ...
- RSync实现文件备份同步
[rsync实现网站的备份,文件的同步,不同系统的文件的同步,如果是windows的话,需要windows版本cwrsync] 一.什么是rsync rsync,remote synchronize顾 ...
- thinkphp分页二,分装到funciton.php
function.php代码 <?php /* 全局分页 * $table 数据表名 * $order 排序 * $pagesize 每页显示N个 * $where 查询条件 * $rollPp ...
- Top 15 Java Utility Classes
In Java, a utility class is a class that defines a set of methods that perform common functions. Thi ...