题意:有N个点和N层..一层有X个点(0<=X<=N).两邻两层间有一条路花费C。还有M条小路在两个点之间。问从第一个点走到第N个点最短路是多少...

可以考虑在每一层增加一个点,这个点到上下层的距离是C,与本层的距离是0;

T的很惨,不太明白为什么,翻了一下大神的博客,发现这个要把每层拆成两个点来算的,要是只拆成一个点那么本层到本层的点都会是0了

////////////////////////////////////////////////////
时间卡的很恶心,还需要双端队列,如果新加入的点的值比队列顶端的额值小就放在方面,否则就放在下面(不明白优化在那里了.......)
#include<stdio.h>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std; const int maxn = 1000005;
const int maxm = 100005;
const int oo = 0xfffffff; struct node
{
    int u, v, c, next;
}e[maxn];
int head[maxm*3], dis[maxm*3];
bool use[maxm*3]; void Add(int u, int v, int w, int k)
{
    e[k].u = u;
    e[k].v = v;
    e[k].c = w;
    e[k].next = head[u];
    head[u] = k;
}
void spfa()
{
    deque<int> Q;
    Q.push_back(1);     while(Q.size())
    {
        int i = Q.front();Q.pop_front();
        use[i] = false;         for(int j=head[i]; j!=0; j=e[j].next)
        {
            int u=e[j].u, v=e[j].v, w=e[j].c;             if(dis[u]+w < dis[v])
            {
                dis[v] = dis[u] + w;                 if(use[v] == false)
                {
                    use[v] = true;
                    if(Q.size() && dis[v] < dis[Q.front()])
                        Q.push_front(v);
                    else
                        Q.push_back(v);
                }
            }
        }
    }
} int main()
{
    int T, t=1;     scanf("%d", &T);     while(T--)
    {
        int i, N, M, C, u, v, w, x, k=1;         scanf("%d%d%d", &N, &M, &C);         memset(head, 0, sizeof(head));         for(i=1; i<=N; i++)
        {
            //本层拆出来的点是 出i+N, 入i+2*N             dis[i] = dis[i+N] = dis[i+2*N] = oo;             if(i != N)
            {
                Add(i+N, i+2*N+1, C, k++);
                Add(i+N+1, i+2*N, C, k++);
            }             scanf("%d", &x);//节点i属于第x层             Add(i, x+N, 0, k++);
            Add(x+2*N, i, 0, k++);
        }         for(i=1; i<=M; i++)
        {
            scanf("%d%d%d", &u, &v, &w);
            Add(u, v, w, k++);
            Add(v, u, w, k++);
        }         dis[1] = 0;
        spfa();         if(dis[N] == oo)
            printf("Case #%d: -1\n", t++);
        else
            printf("Case #%d: %d\n", t++, dis[N]);
    }     return 0;
}

P - The Shortest Path in Nya Graph-hdu4725(双端队列+拆点)的更多相关文章

  1. HDU4725:The Shortest Path in Nya Graph(最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  2. HDU-4725 The Shortest Path in Nya Graph (拆点+dji)

    HDU 4725 The Shortest Path in Nya Graph : http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意: 在一个图中跑最 ...

  3. HDU 4725 The Shortest Path in Nya Graph(构图)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  4. HDU 4725 The Shortest Path in Nya Graph (最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. Hdu 4725 The Shortest Path in Nya Graph (spfa)

    题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...

  6. HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]

    HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...

  7. HDU 4725 The Shortest Path in Nya Graph

    he Shortest Path in Nya Graph Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged o ...

  8. hdu 4725 The Shortest Path in Nya Graph (最短路+建图)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. HDU - 4725_The Shortest Path in Nya Graph

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

随机推荐

  1. 类和ID选择器的区别

    学习了类选择器和ID选择器,我们会发现他们之间有很多的相似处,是不是两者可以通用呢?我们不要着急先来总结一下他们的相同点和不同点: 相同点:可以应用于任何元素不同点: 1.ID选择器只能在文档中使用一 ...

  2. CoreBluetooth

    Core Bluetooth的基本常识 每个蓝牙4.0设备都是通过服务(Service)和特征(Characteristic)来展示自己的 一个设备必然包含一个或多个服务,每个服务下面又包含若干个特征 ...

  3. 07ADO.Net

    1.ADO.Net简介 代码示例: using (MySqlConnection conn = new MySqlConnection("Server=localhost;Database= ...

  4. JS & JQuery 动态添加 select option

    因为是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接有时候会失效,所以 ...

  5. elastic search 学习笔记

    Elastic search在数据分析的应用中相当于一个数据库的搜索引擎. 跟MySQL类似,它有自己的查询语言,只不过不是关系型数据库,属于NoSQL. 可以根据索引从分布式服务器文件系统中快速存取 ...

  6. windows core audio apis

    这个播放流程有一次当初不是很理解,做个记录,代码中的中文部分,原文档是有解释的:To move a stream of rendering data through the endpoint buff ...

  7. free 堡垒机

    环境: centos6.5 mini安装 iptables selinux已经关闭 jumpserver: 192.168.1.209 testserver: 192.168.1.210 一. 部署l ...

  8. 关掉PUTTY后,进程仍可以运行。

    如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令.该命令可以在你退出帐户之后继续运行相应的进程.no hup就是不挂起的意思( no hang up).该命令 ...

  9. zoj3839-Poker Face

    #include<cstdio>int n;void P(int i,int j,int n,int f){ if(i==n){ for(int k=1;k<=n;k++)print ...

  10. jQuery API中文文档

    jQuery API中文文档 http://www.css88.com/jqapi-1.9/category/events/event-handler-attachment/ jQuery UI AP ...