https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392

A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input:

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Sample Output:

0 2 3 3 4

代码:

#include <bits/stdc++.h>
using namespace std; #define inf 0x3f3f3f3f int N, M, S, D;
int mp[550][550], cost[550][550];
int dis1[550],dis2[550],dis3[550];
int vis[550];
int pre[550]; int MinStep, out = INT_MAX; void dijkstra(int S, int dis[]) {
dis[S] = 0;
memset(vis, 0, sizeof(vis));
int temp = S; for(int i = 0; i < N; i ++) {
int minn = inf;
for(int j = 0; j < N; j ++) {
if(dis[j] < minn && vis[j] == 0) {
minn = dis[j];
temp = j;
}
}
vis[temp] = 1;
for(int k = 0; k < N; k ++)
if(vis[k] == 0 && mp[temp][k] != inf) {
if(dis[k] > mp[temp][k] + dis[temp])
dis[k] = mp[temp][k] + dis[temp];
}
}
} void dijkstra(int S) { dis3[S] = 0;
memset(vis, 0, sizeof(vis));
int temp = S; for(int i = 0; i < N; i ++) {
int minn = inf;
for(int j = 0; j < N; j ++) {
if(dis3[j] < minn && vis[j] == 0) {
minn = dis3[j];
temp = j;
}
}
vis[temp] = 1;
for(int k = 0; k < N; k ++)
if(mp[temp][k] + dis1[temp] + dis2[k] == MinStep)
if(vis[k] == 0 && cost[temp][k] != inf)
if(dis3[k] > cost[temp][k] + dis3[temp]){
dis3[k] = cost[temp][k] + dis3[temp];
pre[k] = temp;
}
}
} void output(int d){
if(d==-1) return ;
output(pre[d]);
printf("%d ", d);
} int main() { memset(pre,-1, sizeof(pre));
memset(vis, 0, sizeof(vis));
memset(cost,inf, sizeof(cost)); memset(dis1, inf, sizeof(dis1));
memset(dis2, inf, sizeof(dis2));
memset(dis3, inf, sizeof(dis3)); memset(mp, inf, sizeof(mp)); scanf("%d%d%d%d", &N, &M, &S, &D);
for(int i = 0; i < M; i ++) {
int st, en, dist, val;
scanf("%d%d%d%d", &st, &en, &dist, &val);
mp[st][en] = mp[en][st] = min(dist, mp[en][st]);
cost[st][en] = cost[en][st] = min(val, cost[st][en]);
} dijkstra(S, dis1);
dijkstra(D, dis2); MinStep = dis1[D]; dijkstra(S); output(D);
printf("%d %d\n", dis1[D], dis3[D]);
return 0;
}

  两遍 dijkstra 一上午经历了无数遍点开题目又退出 枯了 睡一会清醒清醒再来写吧

PAT 甲级 1030 Travel Plan的更多相关文章

  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 A 1030. Travel Plan (30)【最短路径】

    https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...

  3. PAT甲级——A1030 Travel Plan

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

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

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

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

  6. 1030 Travel Plan (30 分)

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

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

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

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

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

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

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

随机推荐

  1. docker tomcat 已主机名为日志输出路径

    目的:所有的日志输出到共享存储目录中 方法:将 tomcat 的日志放置到 /data/logs/主机名/  下, 1. 修改tomcat/conf下的logging.properties [root ...

  2. dbca时报错:ORA-12705(NLS_LANG=AMERICAN_AMERICA.UTF8);

    #add by zexport ORACLE_BASE=/u01/oracle export ORACLE_HOME=/u01/oracle/11.02 export ORACLE_SID=z exp ...

  3. [转]OpenCV2.4.12 开启OpenGL启用三维可视化支持

    OpenCV默认情况下是不支持OpenGL的,如果要使OpenCV支持OpenGL,则需要重编译,具体步骤如下: 注意事项:从The OpenCV Reference ManualOpenCV参考手册 ...

  4. C#中使用WeiFenLuo.WinFormsUI.Docking.dll实现窗口停靠效果

    很酷的效果,很值得好好去学习的哈. 重置工具箱: 新建一个WinForm程序,项目名称为TestDockPanelControl.选中Form1窗体后选择工具箱--->>新建个添加选项卡命 ...

  5. SpringBoot实用技巧札记

    目录 如何手工设置SpringBoot内嵌的Tomcat启动端口号(port) 如何解决Eclipse.Properties中文乱码的问题 如何手工设置SpringBoot内嵌的Tomcat启动端口号 ...

  6. C++使用默认参数的构造函数

    我们可以想象一个这样的场景:某一天书店整理库存,发现了一些非常老的书,为了尽快清空库存,店主想了一下,决定开展一个大甩卖活动,所有的这些书全部以五美元的价格出售.此时如果需要尽快将这些书的信息录入到书 ...

  7. Java http协议概述

    一.http协议用于定义客户端与web服务端通讯的格式 二.HTTP1.0与HTTP1.1的区别 1.在HTTP1.0协议中,客户端与web服务器建立链接后只能获取一个web资源 2.HTTP1.1协 ...

  8. RTSP server 在mips 上莫名其妙退出(PC上则无此问题)

    http://blog.csdn.net/lubing20044793/article/details/38523701 早在这篇blog以前写过,在虚拟机下调试sn9c291时,USB 数据传输出了 ...

  9. 【LeetCode206】Reverse Linked List★

    题目描述: 解题思路: 关于单链表的反转有迭代和递归两种方法,方法不在多,本文主要介绍迭代的方法. 迭代的方法,要使用三个指针,需要注意一点的是指针的初始化,对第一个指针初始化为pre=null,第二 ...

  10. [hdu5503]EarthCup[霍尔定理]

    题意 一共 \(n\) 只球队,两两之间会进行一场比赛,赢得一分输不得分,给出每只球队最后的得分,问能否构造每场比赛的输赢情况使得得分成立.多组数据 \(T\le 10,n\le 5\times 10 ...