SAMER08A - Almost Shortest Path

Finding the shortest path that goes from a starting point to a destination point given a set of points and route lengths connecting them is an already well known problem, and it's even part of our daily lives, as shortest path programs are widely available nowadays.

Most people usually like very much these applications as they make their lives easier. Well, maybe not that much easier.

Now that almost everyone can have access to GPS navigation devices able
to calculate shortest paths, most routes that form the shortest path
are getting slower because of heavy traffic. As most people try to
follow the same path, it's not worth it anymore to follow these
directions.

With this in his mind, your boss asks you to develop
a new application that only he will have access to, thus saving him
time whenever he has a meeting or any urgent event. He asks you that the
program must answer not the shortest path, but the almost shortest
path. He defines the almost shortest path as the shortest path that goes
from a starting point to a destination point such that no route between
two consecutive points belongs to any shortest path from the starting
point to the destination.

For example, suppose the figure below
represents the map given, with circles representing location points, and
lines representing direct, one-way routes with lengths indicated. The
starting point is marked as S and the destination point is marked as D.
The bold lines belong to a shortest path (in this case there are two
shortest paths, each with total length 4). Thus, the almost shortest
path would be the one indicated by dashed lines (total length 5), as no
route between two consecutive points belongs to any shortest path.
Notice that there could exist more than one possible answer, for
instance if the route with length 3 had length 1. There could exist no
possible answer as well.

Input

The input contains several test cases. The first line of a test case contains two integers N (2 ≤ N ≤ 500) and M (1 ≤ M ≤ 104),
separated by a single space, indicating respectively the number of
points in the map and the number of existing one-way routes connecting
two points directly. Each point is identified by an integer between 0
and N -1. The second line contains two integers S and D, separated by a single space, indicating respectively the starting and the destination points (SD; 0 ≤ S, D < N).

Each one of the following M lines contains three integers U, V and P (UV; 0 ≤ U, V < N; 1 ≤ P ≤ 103), separated by single spaces, indicating the existence of a one-way route from U to V with distance P. There is at most one route from a given point U to a given point V, but notice that the existence of a route from U to V does not imply there is a route from V to U,
and, if such road exists, it can have a different length. The end of
input is indicated by a line containing only two zeros separated by a
single space.

Output

For each test case in the input, your program must print a single line, containing -1 if it is not possible to match the requirements, or an integer representing the length of the almost shortest path found.

Example

  1. Input:
  2. 7 9
  3. 0 6
  4. 0 1 1
  5. 0 2 1
  6. 0 3 2
  7. 0 4 3
  8. 1 5 2
  9. 2 6 4
  10. 3 6 2
  11. 4 6 4
  12. 5 6 1
  13. 4 6
  14. 0 2
  15. 0 1 1
  16. 1 2 1
  17. 1 3 1
  18. 3 2 1
  19. 2 0 3
  20. 3 0 2
  21. 6 8
  22. 0 1
  23. 0 1 1
  24. 0 2 2
  25. 0 3 3
  26. 2 5 3
  27. 3 4 2
  28. 4 1 1
  29. 5 1 1
  30. 3 0 1
  31. 0 0
  32.  
  33. Output:
  34. 5
  35. -1
  36. 6
  37.  
  38. 题意是给出一个单向图,然后只要这条路径(S->D)的长度和最短路的长度一致,那么这条路上所有的边都删掉之后,再跑一次从S->D的最短路。
    反向建边后跑两次dij,然后枚举所有边将属于最短路的边删掉。
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define inf 0x3f3f3f3f
  4. #define pii pair<int,int>
  5. #define mp make_pair
  6. struct Edge{
  7. int u,v,w,next;
  8. bool o;
  9. }e1[],e2[];
  10. int tot1,tot2,first1[],first2[];
  11. void add1(int u,int v,int w){
  12. e1[tot1].u=u;
  13. e1[tot1].v=v;
  14. e1[tot1].o=;
  15. e1[tot1].w=w;
  16. e1[tot1].next=first1[u];
  17. first1[u]=tot1++;
  18. }
  19. void add2(int u,int v,int w){
  20. e2[tot2].u=u;
  21. e2[tot2].v=v;
  22. e2[tot2].o=;
  23. e2[tot2].w=w;
  24. e2[tot2].next=first2[u];
  25. first2[u]=tot2++;
  26. }
  27. int N,M,S,D,i,j,k;
  28. bool vis[];
  29. int d1[],d2[];
  30. int dij(int S,int D,int d[],Edge e[],int first[]){
  31. memset(d,inf,sizeof(int)*);
  32. memset(vis,,sizeof(bool)*);
  33. priority_queue<pii,vector<pii>,greater<pii> > q;
  34. q.push(mp(,S));
  35. d[S]=;
  36. while(!q.empty()){
  37. int u=q.top().second;
  38. q.pop();
  39. if(vis[u]) continue;
  40. vis[u]=;
  41. for(int i=first[u];i+;i=e[i].next){
  42. if(e[i].o&&d[e[i].v]>d[u]+e[i].w){
  43. d[e[i].v]=d[u]+e[i].w;
  44. q.push(mp(d[e[i].v],e[i].v));
  45. }
  46. }
  47. }
  48. return d[D]==inf?-:d[D];
  49. }
  50. int main()
  51. {
  52. while(cin>>N>>M&&(N||M)){int u,v,w;
  53. cin>>S>>D;
  54. memset(first1,-,sizeof(first1));
  55. memset(first2,-,sizeof(first2));
  56. tot1=tot2=;
  57. while(M--){
  58. scanf("%d%d%d",&u,&v,&w);
  59. add1(u,v,w);
  60. add2(v,u,w);
  61. }
  62. int minn=dij(S,D,d1,e1,first1);
  63. dij(D,S,d2,e2,first2);
  64. for(i=;i<tot1;++i){
  65. if(e1[i].w+d1[e1[i].u]+d2[e1[i].v]==minn) e1[i].o=;
  66. }
  67. cout<<dij(S,D,d1,e1,first1)<<endl;
  68. }
  69. return ;
  70. }
  1.  

spoj-SAMER08A-最短路的更多相关文章

  1. POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

  2. SPOJ OTOCI 动态树 LCT

    SPOJ OTOCI 裸的动态树问题. 回顾一下我们对树的认识. 最初,它是一个连通的无向的无环的图,然后我们发现由一个根出发进行BFS 会出现层次分明的树状图形. 然后根据树的递归和层次性质,我们得 ...

  3. bzoj1001--最大流转最短路

    http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...

  4. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  5. 【USACO 3.2】Sweet Butter(最短路)

    题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...

  6. Sicily 1031: Campus (最短路)

    这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...

  7. 最短路(Floyd)

    关于最短的先记下了 Floyd算法: 1.比较精简准确的关于Floyd思想的表达:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设maz ...

  8. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  9. bzoj1266最短路+最小割

    本来写了spfa wa了 看到网上有人写Floyd过了 表示不开心 ̄へ ̄ 改成Floyd试试... 还是wa ヾ(。`Д´。)原来是建图错了(样例怎么过的) 结果T了 于是把Floyd改回spfa 还 ...

随机推荐

  1. adb push ,adb pull和adb install的区别

    1.用命令行把手机上的文件拷贝到电脑上 1 adb pull sdcard/1222073679.png 拷贝文件夹命令,如把log文件夹拷贝到电脑当前目录 1 adb pull sdcard/log ...

  2. Java并发编程实战3章

    1.同步包括两方面:原子性和可见性. 2.可见性:因为在多线程程序中,如果没有采用正确的同步,有些线程就会得到失效数据. Java内存模型要求,变量的读取操作和写入操作都必须是原子操作,但对于非vol ...

  3. mysql数据库从删库到跑路之mysql基础

    一 数据库是什么 之前所学,数据要永久保存,比如用户注册的用户信息,都是保存于文件中,而文件只能存在于某一台机器上. 如果我们不考虑从文件中读取数据的效率问题,并且假设我们的程序所有的组件都运行在一台 ...

  4. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E Underground Lab

    地址:http://codeforces.com/contest/782/problem/E 题目: E. Underground Lab time limit per test 1 second m ...

  5. GYM - 101490 J Programming Tutors (匈牙利+二分)

    题意:有N个学生和N个老师,每个人都有自己的坐标X,Y,给每个学生匹配一个老师,要求N个匹配中的距离最大值最小.其中距离的定义为:|X − X’| + |Y − Y ‘|. 分析:一道典型的最大值最小 ...

  6. 有关RDD的基础学习1

    1.spark rdd为什么不能嵌套?    譬如 val rdd1=sc.parallel(range(1,100))    val rdd2=sc.parallel(range(1,100))   ...

  7. 在eclipse中new 对象后怎么通过快捷键自动生成返回对象

    如题,每次new 对象的时候不想手动补全返回对象,可以实现快捷键生成返回对象.new  对象后可以按住ctrl+1,如下图: 选择第一行即可.

  8. nginx添加sticky cookie 分流模块

    需要下载nginx源码和sticky,在nginx配置文件中添加sticky模块,然后重新编译nginx. #准备安装基础环境:yum install gcc openssl-devel pcre-d ...

  9. 对于近阶段公司代码 review 小结

    来新公司,给公司的SDK review了一下.发现了不少小问题,在此总结一下. (我下面说明问题可能是很简单,但是搞清楚某些问题还是花了些时间的,大家引以为戒吧) 先谈谈处理的问题: 1.某天QA说有 ...

  10. C++ 第三十三天

    Ⅰ.类成员函数的隐式参数 T *const this . 就是说对于某个类的成员函数 returnType function() 的真实面目其实是这样的 returnType function(T * ...