Source:

PAT A1111 Online Map (30 分)

Description:

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

V1 V2 one-way length time

where V1 and V2 are the indices (from 0 to N−1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance D in the format:

Distance = D: source -> v1 -> ... -> destination

Then in the next line print the fastest path with total time T:

Time = T: source -> w1 -> ... -> destination

In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

Distance = D; Time = T: source -> u1 -> ... -> destination

Sample Input 1:

10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5

Sample Output 1:

Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5

Sample Input 2:

7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5

Sample Output 2:

Distance = 3; Time = 4: 3 -> 2 -> 5

Keys:

Attention:

  • 其实就是求两次最短路径,代码code一遍再copy一遍,考试的时候怎么快怎么来,优化代码反而容易出错

Code:

 /*
Data: 2019-08-18 21:30:18
Problem: PAT_A1111#Online Map
AC: 42:06 题目大意:
给出当前位置和目的地,给出最短路径和最快路径
输入:
第一行给出,结点数N,边数M
接下来M行,v1,v2,单/双向,距离,时间
最后一行,起点,终点
输出:
最短距离,输出路径,不唯一则选择最快的
最短时间,输出路径,不唯一则选择经过结点最少的
*/
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int M=1e3,INF=1e9;
int grap[M][M],cost[M][M],vis[M],d[M],c[M];
int n,m,v1,v2,one,optTime=INF,optLeth=INF;
vector<int> preL[M],preT[M],optL,pathL,optT,pathT; void DijskraL(int s)
{
fill(vis,vis+M,);
fill(d,d+M,INF);
d[s]=;
for(int i=; i<n; i++)
{
int u=-,Min=INF;
for(int j=; j<n; j++)
{
if(vis[j]== && d[j]<Min)
{
Min = d[j];
u = 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])
{
preL[v].clear();
preL[v].push_back(u);
d[v] = d[u]+grap[u][v];
}
else if(d[u]+grap[u][v]==d[v])
preL[v].push_back(u);
}
}
}
} void DFSL(int s)
{
if(s == v1)
{
pathL.push_back(v1);
int time=,len=pathL.size();
for(int i=; i<len; i++)
time += cost[pathL[i]][pathL[i-]];
if(time < optTime)
{
optTime = time;
optL = pathL;
}
pathL.pop_back();
return;
}
pathL.push_back(s);
for(int i=; i<preL[s].size(); i++)
DFSL(preL[s][i]);
pathL.pop_back();
} void DijskraT(int s)
{
fill(vis,vis+M,);
fill(c,c+M,INF);
c[s]=;
for(int i=; i<n; i++)
{
int u=-,Min=INF;
for(int j=; j<n; j++)
{
if(vis[j]== && c[j]<Min)
{
Min = c[j];
u = j;
}
}
if(u==-) return;
vis[u]=;
for(int v=; v<n; v++)
{
if(vis[v]== && cost[u][v]!=INF)
{
if(c[u]+cost[u][v] < c[v])
{
preT[v].clear();
preT[v].push_back(u);
c[v] = c[u] + cost[u][v];
}
else if(c[u]+cost[u][v]==c[v])
preT[v].push_back(u);
}
}
}
} void DFST(int s)
{
if(s == v1)
{
pathT.push_back(v1);
if(pathT.size() < optLeth)
{
optLeth = pathT.size();
optT = pathT;
}
pathT.pop_back();
return;
}
pathT.push_back(s);
for(int i=; i<preT[s].size(); i++)
DFST(preT[s][i]);
pathT.pop_back();
} 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); scanf("%d%d", &n,&m);
for(int i=; i<m; i++)
{
scanf("%d%d%d", &v1,&v2,&one);
scanf("%d%d", &grap[v1][v2],&cost[v1][v2]);
if(!one)
{
grap[v2][v1]=grap[v1][v2];
cost[v2][v1]=cost[v1][v2];
}
}
scanf("%d%d", &v1,&v2);
DijskraL(v1);
DFSL(v2);
DijskraT(v1);
DFST(v2);
if(optT == optL)
{
printf("Distance = %d; Time = %d: %d", d[v2],c[v2],optT[optT.size()-]);
for(int i=optT.size()-; i>=; i--)
printf(" -> %d", optT[i]);
}
else
{
printf("Distance = %d: %d", d[v2], optL[optL.size()-]);
for(int i=optL.size()-; i>=; i--)
printf(" -> %d", optL[i]);
printf("\nTime = %d: %d", c[v2], optT[optT.size()-]);
for(int i=optT.size()-; i>=; i--)
printf(" -> %d", optT[i]);
} return ;
}

PAT_A1111#Online Map的更多相关文章

  1. mapreduce中一个map多个输入路径

    package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...

  2. .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法

    .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...

  3. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  4. Java基础Map接口+Collections

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  5. 多用多学之Java中的Set,List,Map

            很长时间以来一直代码中用的比较多的数据列表主要是List,而且都是ArrayList,感觉有这个玩意就够了.ArrayList是用于实现动态数组的包装工具类,这样写代码的时候就可以拉进 ...

  6. Java版本:识别Json字符串并分隔成Map集合

    前言: 最近又看了点Java的知识,于是想着把CYQ.Data V5迁移到Java版本. 过程发现坑很多,理论上看大部分很相似,实践上代码写起来发现大部分都要重新思考方案. 遇到的C#转Java的一些 ...

  7. MapReduce剖析笔记之八: Map输出数据的处理类MapOutputBuffer分析

    在上一节我们分析了Child子进程启动,处理Map.Reduce任务的主要过程,但对于一些细节没有分析,这一节主要对MapOutputBuffer这个关键类进行分析. MapOutputBuffer顾 ...

  8. MapReduce剖析笔记之七:Child子进程处理Map和Reduce任务的主要流程

    在上一节我们分析了TaskTracker如何对JobTracker分配过来的任务进行初始化,并创建各类JVM启动所需的信息,最终创建JVM的整个过程,本节我们继续来看,JVM启动后,执行的是Child ...

  9. MapReduce剖析笔记之五:Map与Reduce任务分配过程

    在上一节分析了TaskTracker和JobTracker之间通过周期的心跳消息获取任务分配结果的过程.中间留了一个问题,就是任务到底是怎么分配的.任务的分配自然是由JobTracker做出来的,具体 ...

随机推荐

  1. Aggressive Cows 二分

    Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are locat ...

  2. 开源GIS软件 3

    OpenWLANMap 与 OpenStreetMap 项目类似,OpenWLANMap 将变成一个开源的替代产品,提供 WLANs 的数据库.定位信息等. 开源排水管网GIS系统 udpnGIS 邢 ...

  3. Codeforces Round #305 (Div. 2) C题 (数论)

    C. Mike and Frog time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  4. but no declaration can be found for element &#39;aop:aspectj-autoproxy&#39;.

    1.错误描写叙述 Multiple annotations found at this line: - cvc-complex-type.2.4.c: The matching wildcard is ...

  5. 将byte[]转为各种进制的字符串

    /**      * 将byte[]转为各种进制的字符串      * @param bytes byte[]      * @param radix 基数可以转换进制的范围(2-36),从Chara ...

  6. EF学习笔记——生成自定义实体类

    使用EF,采用DataBase 模式,实体类都是按照数据库的定义自动生成,我们似乎无法干预.如果要生成自定义的实体类,该怎么做呢? 思路是这样的: 1.我们要自定义生成的实体类,都是分部类(parti ...

  7. BKDRHash 算法 php 版本( 可用于 字符串 hash 为int 转)

    <?php function BKDRHash($str) { $seed = 131; // 31 131 1313 13131 131313 etc.. $hash = 0; $cnt = ...

  8. 当Shell遇上了Node.js(转载)

    转载:http://developer.51cto.com/art/201202/315066.htm 好吧,我承认,这个标题有点暧昧的基情,但是希望下文的内部能给不熟悉或不喜欢Shell或WIN平台 ...

  9. Arbitrage(floyd)

    http://poj.org/problem?id=2240 #include <stdio.h> #include <string.h> <<; ][]; ][] ...

  10. bzoj 1503郁闷的出纳员(splay)

    1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 11759  Solved: 4163[Submit][Stat ...