HDU4725(spfa+双端队列优化)

Problem 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 <= 105) and C(1 <= C <= 103), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers li (1 <= li <= N), which is the layer of ith node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 104), 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

N个点,然后有N层,要假如2*N个点。 总共是3*N个点。 点1~N就是对应的实际的点1~N.  要求的就是1到N的最短路。 然后点N+1 ~ 3*N 是N层拆出出来的点。 第i层,入边到N+2*i-1, 出边从N+2*i 出来。(1<= i <= N) N + 2*i    到  N + 2*(i+1)-1 加边长度为C. 表示从第i层到第j层。 N + 2*(i+1) 到 N + 2*i - 1 加边长度为C,表示第i+1层到第j层。 如果点i属于第u层,那么加边 i -> N + 2*u -1         N + 2*u ->i  长度都为0

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=300005;
const int INF=0x3f3f3f3f;
int head[maxn];
bool vis[maxn];
int dis[maxn];
int tot;
int N,M,C;
inline int read()
{
  char ls=getchar();for (;ls<'0'||ls>'9';ls=getchar());
  int x=0;for (;ls>='0'&&ls<='9';ls=getchar()) x=x*10+ls-'0';
  return x;
}
struct node
{
    int v,w,next;
    node(){}
    node(int v,int w,int next):v(v),w(w),next(next){}
    bool operator <(const node &rhs)const
    {
        return v > rhs.v;
    }
}E[maxn*5];
void add(int u,int v,int w)
{
    E[tot].v=v;
    E[tot].w=w;
    E[tot].next=head[u];
    head[u]=tot++;
}
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}
void spfa(int st,int ed)
{
    for(int i=1;i<=ed;i++)
    {
        vis[i]=false;
        dis[i]=INF;
    }
    dis[st]=0;
    vis[st]=true;
    int now,next;
    deque<int>q;
    q.push_back(st);
    while(!q.empty())
    {
        now=q.front();
        q.pop_front();
        vis[now]=false;
        for(int i=head[now];i!=-1;i=E[i].next)
        {
            next=E[i].v;
            if(dis[next]>dis[now]+E[i].w)
            {
                dis[next]=dis[now]+E[i].w;
                if(!vis[next])
                {
                    vis[next]=true;
                    q.push_back(next);
                }
            }
        }
    }
}
int main()
{
    int T;
    T=read();
    for(int tem=1;tem<=T;tem++)
    {
        init();
       N=read();
       M=read();
       C=read();
        int u,v,w;
        for(int i=1;i<=N;i++)
        {
            u=read();
            add(i,u*2+N-1,0);
            add(u*2+N,i,0);
        }
        for(int i=1;i<N;i++)
        {
            add(N+2*i-1,N+2*i+2,C);
            add(N+2*i+1,N+2*i,C);
        }
        for(int i=1;i<=M;i++)
        {
            u=read();
            v=read();
            w=read();
            add(u,v,w);
            add(v,u,w);
        }
        spfa(1,3*N);
        if(dis[N]==INF) printf("Case #%d: -1\n",tem);
        else printf("Case #%d: %d\n",tem,dis[N]);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值