HDU

来源:互联网 发布:vb api浏览器 user32 编辑:程序博客网 时间:2024/05/20 01:10

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个点,m条边,每个点在某个层,层与层之间的距离为c,同层之间没有边则不能互达。求1到n的最短路。
开始做的时候想着每层看做一个点,层中所有点到该层看做的点的距离为0,无向边,层与层之间的边为c,结果发现这样做会导致同层的点会通过该层虚拟的点使同层两点距离为0。那么每层虚拟出两个点,一个点出度为1,一个点入度为1,建立层中的点与层点的有向边,这样就不会出现同层距离为0的情况。一共建了3*n个点。使用spfa。
(直接贴kuangbin的代码过来了)

#include <stdio.h>#include <string.h>#include <iostream>#include <vector>#include <queue>#include <string>#include <math.h>#include <stdlib.h>using namespace std;const int INF = 0x3f3f3f3f;const int MAXN = 1000010;struct qnode{    int v;    int c;    qnode(int _v = 0, int _c = 0) :v(_v), c(_c){}    bool operator <(const qnode &r)const    {        return c>r.c;    }};struct Edge{    int v, cost;    Edge(int _v = 0, int _cost = 0) :v(_v), cost(_cost){}};vector<Edge>E[MAXN];bool vis[MAXN];int dist[MAXN];void Dijkstra(int n, int start){    memset(vis, false, sizeof(vis));    for (int i = 1; i <= n; i++)dist[i] = INF;    priority_queue<qnode>que;    while (!que.empty())que.pop();    dist[start] = 0;    que.push(qnode(start, 0));    qnode tmp;    while (!que.empty())    {        tmp = que.top();        que.pop();        int u = tmp.v;        if (dist[u] < tmp.c)continue;        for (int i = 0; i<E[u].size(); i++)        {            int v = E[tmp.v][i].v;            int cost = E[u][i].cost;            if ( dist[v]>dist[u] + cost)            {                dist[v] = dist[u] + cost;                que.push(qnode(v, dist[v]));            }        }    }}void addedge(int u, int v, int w){    E[u].push_back(Edge(v, w));}int main(){    int T;    int N, M, C;    scanf("%d", &T);    int iCase = 0;    while (T--)    {        scanf("%d%d%d", &N, &M, &C);        for (int i = 1; i <= 3 * N; i++) E[i].clear();        int u, v, w;        for (int i = 1; i <= N; i++)        {            scanf("%d", &u);            addedge(i, N + 2 * u - 1, 0);            addedge(N + 2 * u, i, 0);        }        for (int i = 1; i < N; i++)        {            addedge(N + 2 * i - 1, N + 2 * (i + 1), C);            addedge(N + 2 * (i + 1) - 1, N + 2 * i, C);        }        while (M--)        {            scanf("%d%d%d", &u, &v, &w);            addedge(u, v, w);            addedge(v, u, w);        }        Dijkstra(3 * N, 1);        iCase++;        if (dist[N] == INF)dist[N] = -1;        printf("Case #%d: %d\n", iCase, dist[N]);    }    return 0;}
原创粉丝点击