最短路-------P - The Shortest Path in Nya Graph

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.
Input
The first line has a number T (T <= 20) , indicating the number of test cases.
For each test case, first line has three numbers N, M (0 <= N, M <= 10 5) and C(1 <= C <= 10 3), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers l i (1 <= l i <= N), which is the layer of i th node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 10 4), which means there is an extra edge, connecting a pair of node u and v, with cost w.
Output
For test case X, output "Case #X: " first, then output the minimum cost moving from node 1 to node N.
If there are no solutions, output -1.
Sample Input
2
3 3 3
1 3 2
1 2 1
2 3 1
1 3 3

3 3 3
1 3 2
1 2 2
2 3 2
1 3 4
Sample Output
Case #1: 2
Case #2: 3

这道题是个好题,主要是建图,建完图跑一遍spfa或者优先队列优化的spfa。。。。。
怎么建图呢,特殊点一定会建个双向边,如果找每两个点之间是否可以连边(符合层数相差1即可),属于一个层的可能有多个点,所以排序弄也得n^2。。。。。
所以多建几个n个点,把层当做一个点,这样O(n)就处理了,每个层点到层内的节点建单向边,边长为0;每个点对相邻的层点建边,这样就可以通过找层点,然后层点找节点,这样两个节点就间接地建立联系了。。。。。。
建好图之后跑一遍spfa即可
vector死慢,用vector超时了,改用的前向星

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1);
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N = 2 * 1e5 + 5;

int n,m,c;
int dis[N];
bool vis[N];
int a[N];
int p = 0;
int head[N];
typedef struct Edge{
    int u,v,w,nxt;
}Edge;
Edge edge[20 * N];

void addEdge(int u,int v,int w)
{
    edge[p].u = u;
    edge[p].v = v;
    edge[p].w = w;
    edge[p].nxt = head[u];
    head[u] = p++;
}

void spfa(int s)
{
    memset(dis,inf,sizeof(dis));
    memset(vis,false,sizeof(vis));
    queue<int>que;
    vis[s] = true;
    dis[s] = 0;
    que.push(s);
    while(!que.empty())
    {
        int tmp = que.front();
        que.pop();
        vis[tmp] = false;
        for(int i = head[tmp];i != -1;i = edge[i].nxt)
        {
            int to = edge[i].v;
            int val = edge[i].w;
            if(dis[tmp] + val < dis[to]){
                dis[to] = dis[tmp] + val;
                if(!vis[to]){
                    vis[to] = true;
                    que.push(to);
                }
            }
        }
    }
}

int main()
{
    //freopen("out.txt","r",stdin);
    int t;
    scanf("%d",&t);
    int cnt = 0;
    while(t--)
    {
        cnt++;
        p = 0;
        memset(head,-1,sizeof(head));
        scanf("%d %d %d",&n,&m,&c);
        for(int i = 1;i <= n;++i){
            scanf("%d",&a[i]);
        }
        for(int i = 1;i <= n;++i){
            //层点与本层的点距离为0
            addEdge(n + a[i],i,0);
            //点到相邻层,不管出没出现过,都建一条边
            if(a[i] > 1){
                addEdge(i,n + a[i] - 1,c);
            }
            if(a[i] < n){
                addEdge(i,n + a[i] + 1,c);
            }
        }
        for(int i = 0;i < m;++i){
            int u,v,w;
            scanf("%d %d %d",&u,&v,&w);
            addEdge(u,v,w);
            addEdge(v,u,w);
        }
        spfa(1);
        if(dis[n] == inf){
            printf("Case #%d: -1\n",cnt);
        }else{
            printf("Case #%d: %d\n",cnt,dis[n]);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36386435/article/details/82946945