题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725

题意:n个点,某个点属于某一层。共有n层。第i层的点到第i+1层的点和到第i-1层的点的代价均是C。另外有一类边连接两个点u、v,代价w。求1到n的最短路。

思路:拆点。n个点不动,编号1到n。将n层每层拆成两个点,第i层拆成n+i*2-1,n+i*2。相邻的层连边(n+i*2-1,n+(i+1)*2,C),(n+(i+1)*2-1,n+i*2,C)。若顶点u属于第i层,连边(u,n+i*2-1,0),(n+i*2,u,0)。再加上另外的那种边。最后用优先队列跑最短路。

vector<pair<int,int> > g[N];
int a[N];
int f[N],n,m,C;

struct node
{
    int u,cost;

    node(){}
    node(int _u,int _cost)
    {
        u=_u;
        cost=_cost;
    }

    int operator<(const node &a) const
    {
        return cost>a.cost;
    }
};

int cal()
{
    priority_queue<node> Q;
    Q.push(node(1,0));
    int i;
    FOR1(i,n*3) f[i]=INF;
    f[1]=0;
    while(!Q.empty())
    {
        int u=Q.top().u;
        Q.pop();

        if(u==n) return f[u];

        FOR0(i,SZ(g[u]))
        {
            int v=g[u][i].first;
            int w=g[u][i].second;
            if(f[u]+w<f[v])
            {
                f[v]=f[u]+w;
                Q.push(node(v,f[v]));
            }
        }
    }
    return -1;
}

void add(int u,int v,int w)
{
    g[u].pb(MP(v,w));
}

int main()
{
    int num=0;
    rush()
    {
        RD(n,m,C);
        int i;
        FOR1(i,n*3) g[i].clear();
        FOR1(i,n-1)
        {
            add(n+i*2-1,n+(i+1)*2,C);
            add(n+(i+1)*2-1,n+i*2,C);
        }
        FOR1(i,n)
        {
            RD(a[i]);
            add(i,n+a[i]*2-1,0);
            add(n+a[i]*2,i,0);
        }
        FOR1(i,m)
        {
            int u,v,w;
            RD(u,v,w);
            add(u,v,w);
            add(v,u,w);
        }
        int ans=cal();
        printf("Case #%d: %d\n",++num,ans);
    }
}

  

HDU 4725 The Shortest Path in Nya Graph(最短路拆点)的更多相关文章

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

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

  2. 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 ...

  3. 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 ...

  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 (最短路)

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

  6. 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 ...

  7. (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  8. HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  9. HDU 4725 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 ...

  10. HDU - 4725 The Shortest Path in Nya Graph 【拆点 + dijkstra】

    This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...

随机推荐

  1. Msys+Mingw在手 妙用在心!

    1 缘起 平时在一些c++群面,看见很多大学十分努力的学习c++/MFC ,看见在编程语言百花争芳的时候,C/C++还是很有很有魅力.估计很多初学者使用都是window下的visual stdio 开 ...

  2. DB天气app冲刺二阶段第八天

    今天突然感觉应该做收尾工作了 因为马上就要考试了,时间一下子就不够用了.. 今天主要修复了一下bug,然后天气基本能够实时准确了,就是多功能按钮还是没有弄好 准备简化一下功能. 明天看看还有什么需要改 ...

  3. 所有的代码生成器都是浮云,如果可以用aspx文件作为模板

    首先申明:标题中的如果是可以去掉的. 想写这篇文章很长时间了,一来是跟大家分享一下,别浪费时间在写代码生成器上面了,什么CodeSmith,XXCodeGenerator等等,都是浮云:二来想跟大家交 ...

  4. 在ubuntu16.04 下安装haproxy 1.5.11 做tcp负载均衡

    由于haproxy需要FQ下载,所以从csdn下载了较为新版的haproxy1.5.11,安装过程如下: 1. 解压haproxy-1.5.11.tar.gz : tar xzvf haproxy-1 ...

  5. SQL SERVER时间函数

    本篇文章还是学习<程序员的SQL金典>内容的记录,此次将讲解的是SQL SERVER的时间函数. 本文只讲SQL SERVER支持的时间函数(其它数据库这里就不罗列了,想看更多的可以关注& ...

  6. 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3

    // test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  7. 【POJ】【2960】S-Nim

    博弈论 这题跟 BZOJ 1874 取石子游戏 差不多 先暴力求出10000以内的SG函数(利用定义来求即可) 然后每次询问直接将SG值异或起来即可…… Source Code Problem: Us ...

  8. [设计模式] 7 适配器模式 adapter

    在 Adapter 模式的结构图中可以看到,类模式的 Adapter 采用继承的方式复用 Adaptee的接口,而在对象模式的 Adapter 中我们则采用组合的方式实现 Adaptee 的复用 类模 ...

  9. Unity3D开发之“获取IOS设备所在的国家代码"

    原地址:http://dong2008hong.blog.163.com/blog/static/469688272014021025578/ 在前一段时间游戏开发中需要实现获取IOS设备所在的国家代 ...

  10. VisualSvn Server介绍

    1 .VisualSvn Server  VisualSvn Server是免费的,而VisualSvn是收费的.VisualSvn是Svn的客户端,和Visual Studio集成在一起,但是不免费 ...