题目链接: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. docker 感性介绍

    Docker 允许开发者们将他们的应用打包放在云端的“容器”中,无需再修改就可以发布到任何流行的 Linux 机器上.由于采用沙盒机制,各应用之间没有任何接口,所以不用担心它们会相互干扰.也同样因为这 ...

  2. php中函数内使用static修饰变量

    首先理解静态变量的区别,声明一个函数test() function num(){ $a = 0; echo $a; $a++; } num();num();num(); //输出000 functio ...

  3. MongoDB工具MagicMongoDBTool

    MagicMongoDBTool工具是一款MongoDB的数据库管理工具,用来进行简单的数据库管理工作. 此工具为国人开发,项目地址:MagicMongoDBTool,目前作者已经完成基本功能开发. ...

  4. UIScrollView显示超出屏幕大小的内容

    1:UIScrollView在初始化的时候可以指定frame,它的frame和其他控件不太一样的地方是: 它的frame指的的“可视大小”,也就是我们指定多大的frame来看一张图片,当然最大就是屏幕 ...

  5. iOS8中的UIAlertController

    转:      iOS8推出了几个新的“controller”,主要是把类似之前的UIAlertView变成了UIAlertController,这不经意的改变,貌似把我之前理解的“controlle ...

  6. Ui设计哪里有好的素材

    刚看到花瓣网,的确不错,以后得多逛逛了.(不喷广告,只留作笔记)

  7. xubuntu install nodejs

    1.安装依赖sudo apt-get install g++ curl libssl-dev apache2-utils git-core 2.去官网获取最新版本 sudo wget http://n ...

  8. 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)

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

  9. Windows ftp 连不上Linux

    1.首先检查vsftpd服务器 是否安装 $ rpm -qa | grep vsftpdvsftpd-2.2.2-11.el6.x86_64 2.显示已安装,restart the vsftpd ¥ ...

  10. 层次数据结构字符串处理,split函数使用

    String str1 = "11@22#33,44,55,#bb#cc,dd,ee,#@DDD@TTT#999,#@"; String[] CX = str1.split(&qu ...