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 ...
随机推荐
- SQL0946N错误及DB2事务日志
在对DB2数据库进行批量增删的时候, 如果数据量比较大会导致SQL0964N错误, DB2 Knowledge center(http://pic.dhe.ibm.com/infocenter/db2 ...
- onmousewheel跟onscroll的区别
研习DOM中的事件方法,发现一些细微差别,百度一下没有类似的解答,随手记下来. onmousewheel事件 以下摘自http://help.dottoro.com/ljmracjb.php Bro ...
- Outlook2016 新装进阶操作指南
启动图片自动下载 键盘上同时按下WIN+R,弹出运行输入框,输入outlook,回车后打开Outlook 依次点击左上角文件,选项,信任中心,信任中心设置,自动下载 在窗口右边反勾选"在HT ...
- bzoj2503&poj3387[NEERC2006]IdealFrame
其实只是把别人的题解强行扩写了 写这篇题解之前我不会的预备知识: 欧拉通路:从图中一个点出发不重复地遍历所有边的路径(可以停在另一个点) 欧拉回路:从图中一个点出发不重复地遍历所有边的回路(必须回到出 ...
- How to stop pycharm show files in project in red color?
You can change the file color to whatever you want. File > Settings > Editor > Colors&F ...
- pthread_cond_signal惊群现象
1.如下代码所示: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include < ...
- C#之方法的声明与调用
//params关键字使用 class Program { static void Main(string[] args){ , , , }; Console.WriteLine(AddFunctio ...
- Npoi导入导出Excel操作
之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...
- 【WIN10】绑定x:Bind
在WP8.WP8中,我们知道有一个绑定{Binding},而在Win10中,新增了一个绑定{x:Bind} x:Bind :为编译时绑定 ,内存.内存相对于传统绑定都有优化 特性: 1.为强类型 ...
- width的数值为百分比
对于width的数值为百分比的时候,表示该元素的长度是相对于父容器来算的. 对于padding-right和padding-left比较好理解也是相对于父容器来算的,但容易出错的是padding-to ...