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)【最短路径】的更多相关文章

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

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

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

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

  4. PAT (Advanced Level) 1030. Travel Plan (30)

    先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath& ...

  5. PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径

    模板题最短路+输出路径如果最短路不唯一,输出cost最小的 #include <iostream> #include <cstdio> #include <algorit ...

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

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

  7. 1030. Travel Plan (30)

    时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...

  8. PAT 甲级 1030 Travel Plan

    https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392 A traveler's map gives ...

  9. 1030 Travel Plan (30)(30 分)

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

随机推荐

  1. ORB-SLAM(四)追踪

    最近在读ORB-SLAM的代码,虽然代码注释算比较多了,但各种类和变量互相引用,看起来有点痛苦.索性总结了一下Tracking部分的代码结构,希望能抓住主要思路,不掉坑里. 追踪 追踪部分的主要思路是 ...

  2. signalr遇到的问题汇总

    1.signalr不会触发 hub类的连接事件和断开连接事件 解决:当时因为我引用的是最新的类库 .当时想到的是类库版本问题.就将他换成官方demo一模一样 发现还是不行..然后用官方demo的客户端 ...

  3. COGS396. [网络流24题]魔术球问题(简化版

    问题描述: 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为 1,2,3,4......的球. (1)每次只能在某根柱子的最上面放球. (2)在同一根柱子中,任何2个相邻球的编号之和为完全平 ...

  4. kafka入门:简介、使用场景、设计原理、主要配置及集群搭建(转)

    问题导读: 1.zookeeper在kafka的作用是什么? 2.kafka中几乎不允许对消息进行"随机读写"的原因是什么? 3.kafka集群consumer和producer状 ...

  5. php桥接设计模式

    <?php //桥接模式 abstract class info{ protected $send=null; public function __construct($send){ $this ...

  6. Web前端开发高手进阶

     Web前端开发高手进阶 js框架+Ajax技术01.初识javascript及其语言基础(一)02.初识javascript及其语言基础(二)03.初识javascript及其语言基础(三)及js原 ...

  7. jquery,php之间的ajax关系以及json

    1.最简介写法 function AjaxDepotGoods(id){ $.ajax({ url:"{:U('stock/depot_goods')}", success:fun ...

  8. GIFT-EMS礼记----青软S2SH(笔记)

    这个S2SH的项目,是这本书的一个贯穿项目,所以这里要记录一下, 看这个项目有两个目的: 1.借助这个项目,学习一下S2SH的综合配置及使用 2.借助这个项目练习一下如何做需求分析和项目架构设计. P ...

  9. windows 10卸载自带软件

    http://jingyan.baidu.com/article/14bd256e4ad268bb6c26126d.html http://jingyan.baidu.com/album/ae97a6 ...

  10. 清北学堂模拟赛day7 石子合并加强版

    /* 注意到合并三堆需要枚举两个端点,其实可以开一个数组记录合并两堆的结果,标程好像用了一个神奇的优化 */ #include<iostream> #include<cstdio&g ...