题意:有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. index ffs、index fs原理考究-1109

    h2 { margin-top: 0.46cm; margin-bottom: 0.46cm; direction: ltr; line-height: 173%; text-align: justi ...

  2. CSS布局模型思考

    flow模型:默认布局模型,元素从左向右.从上到下依次排列,块状元素独占一行.Position属性对应值static. float模型:主要效果是让本来独占一行的块状元素变成内联-块状元素,并到一排显 ...

  3. 【BZOJ1036】【树链剖分】树的统计Count

    Description 一 棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. ...

  4. 【POJ2761】【fhq treap】A Simple Problem with Integers

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  5. grunt live

    { "name": "grunt-live-test", "version": "0.1.0", "autho ...

  6. JS自执行函数的几种写法

    一:整体写在一个括号中 代码如下: (function Show(){alert("hello");}()) 二:function函数整体外加括号 代码如下: (function ...

  7. linux配置记录

    今天想把开发环境都配置到linux环境来,所以用wubi挂载了个ubuntu系统(64位),因为不常使用linux所以把今天学到的一些 东西记下来以做备查. #1.   java环境配置 到oracl ...

  8. 修改app名字

    一张图说明问题 如果没有成功clean一下,或者卸载掉原有的重新生成一下 如果要修改路径名和工程名有个复杂的方法 http://blog.sina.com.cn/s/blog_a42013280101 ...

  9. Python3 time()

    在<Python基础教程(第二版)>一书中, if time % 60 == 0 : print 'on the hour! '在3.3.2版本中显示错误.于是自己查了一下帮助文档,也在网 ...

  10. BZOJ 1014 火星人prefix

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...