The Shortest Path in Nya Graph(神一样的建图)

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.

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
F**K

每个点都属于一个层数,相邻层是可以互相移动的,点之间也是有边相连的,现在求1到n的最短路。
如果只用n个点就炸了!这样建图边的数量太多了。(每个层不止一个点)
然后机智的分出n各点,代表层数,然后点连向对应的层,但是还是容易炸。
所以就再出n个点,一个代表入度,一个代表出度,然后就是建图了。
add_edge(n + 2*i - 1,n + 2*(i + 1),c,cnt++);
add_edge(n + 2*(i + 1) - 1,n + 2*i,c,cnt++);这个需要自己画图理解一下。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
int head[300050],d[300050],n;
bool leap[300050];
struct Node
{
    int u,v,w,next;
}node[600050];
void add_edge(int u,int v,int w,int tot)
{
    node[tot].u = u;node[tot].v = v;
    node[tot].w = w;
    node[tot].next = head[u];
    head[u] = tot;
}
void spfa()
{
    for(int i = 1;i <= 3*n;i++)leap[i] = false;
    for(int i = 1;i <= 3*n;i++)d[i] = inf;
    d[1] = 0;leap[1] = true;
    deque<int>p;
    p.push_back(1);
    while(p.size())
    {
        int u = p.front();p.pop_front();
        leap[u] = false;
        for(int i = head[u];i != -1;i = node[i].next)
        {
            int v = node[i].v;
            if(d[v] > d[u] + node[i].w)
            {
                d[v] = d[u] + node[i].w;
                if(!leap[v])
                {
                    leap[v] = true;
                    if(p.size()&&d[v] < d[p.front()])
                    {
                        p.push_front(v);
                    }
                    else
                        p.push_back(v);
                }
            }
        }
    }
}
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\ΡΡ\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\ΡΡ\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    int t,kase = 1;
    scanf("%d",&t);
    while(t--)
    {
        int m,c;
        int cnt = 1;
        scanf("%d%d%d",&n,&m,&c);
        memset(head,-1,sizeof(head));
        for(int i = 1;i <= n;i++)
        {
            int x;scanf("%d",&x);
            add_edge(i,n + 2*x - 1,0,cnt++);
            add_edge(n + 2*x,i,0,cnt++);
        }
        for(int i = 1;i < n;i++)
        {
            add_edge(n + 2*i - 1,n + 2*(i + 1),c,cnt++);
            add_edge(n + 2*(i + 1) - 1,n + 2*i,c,cnt++);
        }
        for(int i = 1;i <= m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add_edge(u,v,w,cnt++);
            add_edge(v,u,w,cnt++);
        }
        spfa();
        if(d[n] == inf || n == 0)
            printf("Case #%d: %d\n",kase++,-1);
        else
            printf("Case #%d: %d\n",kase++,d[n]);
    }
    return 0;
}

2*n个点也是可以过得,就是建图优化一下,点直接连到相邻层,不连到自己那层,可以减少边的个数,然后就过了……莫名A。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
int head[300050],d[300050],n,ran[100050];
bool leap[300050],vis[100050];
struct Node
{
    int u,v,w,next;
}node[600050];
void add_edge(int u,int v,int w,int tot)
{
    node[tot].u = u;node[tot].v = v;
    node[tot].w = w;
    node[tot].next = head[u];
    head[u] = tot;
}
void spfa()
{
    for(int i = 1;i <= 2*n;i++)leap[i] = false;
    for(int i = 1;i <= 2*n;i++)d[i] = inf;
    d[1] = 0;leap[1] = true;
    queue<int>p;
    p.push(1);
    while(p.size())
    {
        int u = p.front();p.pop();
        leap[u] = false;
        for(int i = head[u];i != -1;i = node[i].next)
        {
            int v = node[i].v;
            if(d[v] > d[u] + node[i].w)
            {
                d[v] = d[u] + node[i].w;
                if(!leap[v])
                {
                    leap[v] = true;
                    p.push(v);
                }
            }
        }
    }
}
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\ΡΡ\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\ΡΡ\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    int t,kase = 1;
    scanf("%d",&t);
    while(t--)
    {
        int m,c;
        int cnt = 1;
        scanf("%d%d%d",&n,&m,&c);
        memset(head,-1,sizeof(head));
        memset(vis,false,sizeof(vis));
        for(int i = 1;i <= n;i++)
        {
            scanf("%d",&ran[i]);
            vis[ran[i]] = 1;
        }
        for(int i = 1;i < n;i++)
        {
            if(vis[i]&&vis[i + 1])
            {
                add_edge(n + i,n + i + 1,c,cnt++);
                add_edge(n + i + 1,n + i,c,cnt++);
            }
        }
        for(int i = 1;i <= n;i++)
        {
            add_edge(n + ran[i],i,0,cnt++);
            if(ran[i] > 1)add_edge(i,n + ran[i] - 1,c,cnt++);
            if(ran[i] < n)add_edge(i,n + ran[i] + 1,c,cnt++);
        }
        for(int i = 1;i <= m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add_edge(u,v,w,cnt++);
            add_edge(v,u,w,cnt++);
        }
        spfa();
        if(d[n] == inf || n == 0)
            printf("Case #%d: %d\n",kase++,-1);
        else
            printf("Case #%d: %d\n",kase++,d[n]);
    }
    return 0;
}

网上还有说法是对spfa“优化”一下,其实根本不是优化,只是因为这个图边特别多,采用了近似dij的思想,把入队的位置改变了一下,这题卡spfa,还是老实用dij稳定一些。就不写在这里了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值