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 ...
随机推荐
- 移动端meta
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scal ...
- Java基础-重写方法
一般我们需要在新类上重写,两个类的实现: class Animal{ public void move(){ System.out.println("动物可以移动"); } } c ...
- 为自己的爬虫更换代理和HTML头部
import requestsimport reimport randomimport time class download(): def __init__(self): self.iplist = ...
- mybatis一个怪异的问题: Invalid bound statement not found
ssm中报一下错误: invalid bound statement (not found): me.tspace.pm.dao.userdao.getuser at org.apache.ib ...
- poj3468 A Simple Problem with Integers(zkw区间修改模板)
此题是一道线段树的裸题,这里只是为了保存我的zkw线段树模板 #include <cstdio> #include <cstring> #include <iostrea ...
- MQTT开发笔记之《MQTT Server》
MQTT SERVER 性能测试报告 : http://w3yyb.sinaapp.com/archives/1601各个MQTT SERVER功能列表: http://blog.lenix.xyz/ ...
- 学习Javascript
分别归类为: javascript变量 javascript运算符 javascript数组 javascript流程语句 javascript字符串函数 javascript函数基础 javascr ...
- Express知识整理
开发实例 Express开发实例(1) —— Hello,world! Express开发实例(2) —— Jade模板引擎
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Mac Pro 软件安装/个性化配置 汇总
苹果产品维修 一.Spotlight 搜索程序和文档 Spotlight是最最常用的东西, 类似Windows开始菜单中的搜索. 可以用来搜索文档,也可以搜索本机的程序, 这样可以快速启动. 点击右 ...