题目链接: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. range,shuffle,str_shuffle

    print_r(range(1,20)); 输出,range产生 Array( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ...

  2. ASP.NET MVC 学习第三天

    今天来简单说一下Razor视图引擎语法相关的和视图类. 添加一个MvcTest项目,继续添加一个Home控制器,完成index的视图添加.我们就在index这里分析razor视图引擎.下面是home控 ...

  3. ASP.NET MVC +EasyUI 权限设计(三)基础模块

    请注明转载地址:http://www.cnblogs.com/arhat 在上一章中呢,我们基本上搭建好了环境,那么本章我们就从基础模块开始写起.由于用户,角色,动作三个当中,都是依赖与动作的,所以本 ...

  4. entityframwork

    entityframwork映射部分: public class NorthwindContext : DbContext { public DbSet<CATEGORIES> Categ ...

  5. dom4j增删改查

    //dom4j添加内容,在第一本书上指定位置添加售价 更改保存所有孩子list集合的顺序 @Test public void add1() throws Exception{ //读 SAXReade ...

  6. js获取时间

    var myDate = new Date(); console.log(myDate.toLocaleString()); function getNowFormatDate() { var dat ...

  7. iOS 通过代码关闭应用程序

    //-------------------------------- 退出程序 -----------------------------------------// - (void)exitAppl ...

  8. Window 8.1 开启Wifi共享

    p{padding-left:20px;} Hosted network supported:Yes  支持Wifi共享 命令:netsh wlan set hostednetwork mode=al ...

  9. What is the Best Programming Language to Learn in 2014?

    It’s been a year since I revealed the best languages to learn in 2013. Once again, I’ve examined the ...

  10. POJ 1988 Cube Stacking(带权并查集)

    哈哈,一次AC. 题意:给你 1-n 编号的立方体,然后移动包含指定编号的立方体的堆移到另一个堆上边, 询问指定的编号立方体下面有多少个立方体. 思路:由于并查集是存储的是它的父亲,那么只能从父亲那里 ...