Description
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do not understand a word of this paragraph, just move on. The Nya graph is an undirected graph with "layers". Each node in the graph belongs to a layer, there are N nodes in total. You can move from any node in layer x to any node in layer x + 1, with cost C, since the roads are bi-directional, moving from layer x + 1 to layer x is also allowed with the same cost. Besides, there are M extra edges, each connecting a pair of node u and v, with cost w. Help us calculate the shortest path from node 1 to node N.
题意就是求最短路问题,但是有一个层的概念,刚开始的时候是想着把相邻两层之间的每一点都再连一条边,但是这样的复杂度可以到达 n^2 ,比如在相邻两层上都有 n/2 个点。
然后百度了之后,发现是要增加点,表示每一层,这个点和同层的连边的边权为0,这样的话两层之间就只需要再加一条边,但是这样还有一个问题,就是同层点之间来往的话,应该是到达相邻的层然后再回来,所以需要在每一层增加两点,分别表示入的和出的。。。
还有一个坑点,就是如果某一层没有点的话,是不能与相邻层连线的。。。
代码如下:
#include#include #include #include #include #include #include #include