Source:

PAT A1030 Travel Plan (30 分)

Description:

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 40

Keys:

Code:

 /*
Data: 2019-06-19 14:09:44
Problem: PAT_A1030#Travel Plan
AC: 28:02 题目大意:
求花费最小的最短路径
*/
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int M=,INF=1e9;
int grap[M][M],cost[M][M],vis[M],d[M],c[M];
int n,st,dt,pre[M]; void Dijskra(int s)
{
for(int i=; i<n; i++)
pre[i]=i;
fill(vis,vis+M,);
fill(d,d+M,INF);
fill(c,c+M,INF);
d[s]=;
c[s]=;
for(int i=; i<n; i++)
{
int u=-,Min=INF;
for(int j=; j<n; j++)
{
if(vis[j]== && d[j]<Min)
{
u = j;
Min = d[j];
}
}
if(u==-) return;
vis[u]=;
for(int v=; v<n; v++)
{
if(vis[v]== && grap[u][v]!=INF)
{
if(d[u]+grap[u][v] < d[v])
{
d[v] = d[u]+grap[u][v];
c[v] = c[u]+cost[u][v];
pre[v]=u;
}
else if(d[u]+grap[u][v]==d[v] && c[u]+cost[u][v]<c[v])
{
c[v] = c[u]+cost[u][v];
pre[v]=u;
}
}
}
}
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE fill(grap[],grap[]+M*M,INF);
fill(cost[],cost[]+M*M,INF); int m,v1,v2;
scanf("%d%d%d%d", &n,&m,&st,&dt);
for(int i=; i<m; i++)
{
scanf("%d%d",&v1,&v2);
scanf("%d%d", &grap[v1][v2],&cost[v1][v2]);
cost[v2][v1]=cost[v1][v2];
grap[v2][v1]=grap[v1][v2];
}
Dijskra(dt);
int s=st;
while(st != dt)
{
printf("%d ", st);
st = pre[st];
}
printf("%d %d %d", dt,d[s],c[s]); return ;
}

PAT_A1030#Travel Plan的更多相关文章

  1. PAT1030 Travel Plan (30)---DFS

    (一)题意 题目链接:https://www.patest.cn/contests/pat-a-practise/1030 1030. Travel Plan (30) A traveler's ma ...

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

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

  3. 1030 Travel Plan (30 分)

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

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

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

  5. PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS

    PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...

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

  7. HDU 4014 Jimmy’s travel plan(图计数)

    Jimmy’s travel plan Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  8. 【JZOJ5081】【GDSOI2017第三轮模拟】Travel Plan 背包问题+双指针+树的dfs序

    题面 100 注意到ban的只会是一个子树,所以我们把原树转化为dfs序列. 然后题目就转化为,询问一段ban的区间,之后的背包问题. 比赛的时候,我想到这里,于是就开始想区间合并,于是搞了线段树合并 ...

  9. PAT A 1030. Travel Plan (30)【最短路径】

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

随机推荐

  1. Q - Period II

    For each prefix with length P of a given string S,if S[i]=S[i+P] for i in [0..SIZE(S)-p-1], then the ...

  2. 如何拿CSDN博客上的原图

    比如带水印的地址: http://img.blog.csdn.net/20140408122234546?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdT ...

  3. RDS MySQL 表上 Metadata lock 的产生和处理

    https://help.aliyun.com/knowledge_detail/41723.html?spm=5176.7841698.2.18.vNfPM3

  4. 固定一个div在浏览器底部

    转自原文 如何固定一个div在浏览器底部   方法1:使用CSS绝对定位 div{ position:absolute; bottom:0px; left:0px; } 方法2:使用CSS固定定位 d ...

  5. FineReport实线java报表填报录入的效果图

    Java报表-固定资产(增删改) Java报表-集团財务报表 Java报表-简单自由填报 Java报表-客户跟踪数据回填 Java报表-客户关系复杂填报 Java报表-批量导入 Java报表-批量删除 ...

  6. STM32的独立看门狗

    STM32 内 部自带了 2 个看门狗:独立看门狗(IWDG)和窗体看门狗(WWDG) STM32 的独立看门狗由内部专门的 40Khz 低速时钟驱动.即使主时钟发生问题.它也仍然 有效. 这里须要注 ...

  7. 虚拟化(四):vsphere高可用功能前提-共享存储搭建

    虚拟化(一):虚拟化及vmware产品介绍 虚拟化(二):虚拟化及vmware workstation产品使用 虚拟化(三):vsphere套件的安装注意及使用 虚拟化(四):vsphere高可用功能 ...

  8. Top10Servlet

    <span style="font-size:18px;">/** * Top10 * author:杨鑫 */ package servlet; import jav ...

  9. 专业函数画图软件Origin

    首先:Origin软件已经是科研院所等单位的必备工作软件之中的一个,之所以大家讨论得较少,有可能并非其上手难度低.而是这些使用人群的学习理解能力要相对高一点吧: 其次:Excel不垃圾,但在函数画图方 ...

  10. Java类集-list

    Collection 子接口: ArrayList是List 接口和Collection接口的一个子类,用于实例化两种接口 package leiji; import java.util.ArrayL ...