题目链接: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. .NET4.5中WCF中默认生成的basicHttpsBinding的研究

    起因: 使用.net4.5建立了一个空白的WCF服务.默认使用的绑定配置是basicHttpsBinding. 问题发现: 1.用客户端进行服务引用,生成了默认的配置文件,其中绑定配置是basicHt ...

  2. iOS常见问题(2)

    一.模拟器黑屏 解决方法: 二.打代码时,Xcode没提示 解决方法: 0. 点击Preferences 1. 进入Text Editing 2. 勾选 三.有时候可能在勾选 Autolayout的时 ...

  3. 显示 mac 隐藏文件

    显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏Mac隐藏文件的命令:defaults writ ...

  4. VBS基础篇 - 运算符

    VBScript 有一套完整的运算符,包括算术运算符.比较运算符.连接运算符和逻辑运算符. 运算符优先级: 首先计算算术运算符,然后计算比较运算符,最后计算逻辑运算符. 所有比较运算符的优先级相同,即 ...

  5. Ubuntu 12.4 下升级 Subversion 1.7

    Ubuntu 12.04 默认使用的是Subversion 1.6,而Ubutnu12.10开始,就使用的是Subversion 1.7. 如果从别人的地方拷过来的SVN目录,在使用SVN命令时会报以 ...

  6. JDBC 学习笔记(一)—— 基础知识 + 分页技术

    本文目录:  1.JDBC简介       2.使用JDBC的步骤——第一个JDBC程序       3.DriverManager ——加载数据库驱动       4.数据库URL ——标识数据库的 ...

  7. TS流格式(转)

    一 从TS流开始 数字电视机顶盒接收到的是一段段的码流,我们称之为TS(Transport Stream,传输流),每个TS流都携带一些信息,如Video.Audio以及我们需要学习的PAT.PMT等 ...

  8. DataGridView 的cell赋值没有线程间访问的限制吗?

    1.如下代码,对DataGridView 的cell赋值不会出现线程访问问题,为什么呢? public Form1() { InitializeComponent(); } private void ...

  9. ORA-00257: 归档程序错误。在释放之前仅限于内部连接

    今天发现oracle数据库连不上,报错:ORA-00257: 归档程序错误.在释放之前仅限于内部连接 马上联想到可能是空间满了,一看磁盘目录,果然. 解决方法如下: 1:查看归档日志目录. 登陆账号后 ...

  10. 后缀树(Suffix Tree)

          问题描述:               后缀树(Suffix Tree)   参考资料: http://www.cppblog.com/yuyang7/archive/2009/03/29 ...