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

 有n个点,每个点都有一个层l[i],相邻层的边有一条无向带权边,权值为都为C,另外还有m条边,每条边对应的u v w 

最后求1到n的最小权值和是多少;

如果直接建图的话会TLE;这里把层数扩展为点n+1----n+n;然后在连接各种关系对应的图,最后用spfa求最短路即可,注意扩点之后点的个数;

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <string>
typedef long long LL;
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
#define N 200005 using namespace std; struct node
{
int v, w, Next;
}G[N*]; int n, m, c, dist[N], vis[N], l[N], v[N]; int Head[N], cnt;
void Add(int u, int v, int w)
{
G[cnt].v = v;
G[cnt].w = w;
G[cnt].Next = Head[u];
Head[u] = cnt++;
} int spfa()
{
for(int i=; i<=n*+; i++)
dist[i] = INF;
met(vis, );
queue<int>Q;
Q.push();
vis[] = ;
dist[] = ;
while(!Q.empty())
{
int p = Q.front();Q.pop();
vis[p] = ;
for(int i=Head[p]; i!=-; i=G[i].Next)
{
int q = G[i].v;
if(dist[q] > dist[p]+G[i].w)
{
dist[q] = dist[p]+G[i].w;
if(!vis[q])
{
vis[q] = ;
Q.push(q);
}
}
}
}
return dist[n];
} int main()
{
int T, t = ;
scanf("%d", &T);
while(T--)
{
met(Head, -);
met(v, );
cnt = ; scanf("%d %d %d", &n, &m, &c); for(int i=; i<=n; i++)
{
scanf("%d", &l[i]);
v[l[i]] = ;
} for(int i=; i<n; i++)
{
if(v[i] && v[i+])
{
Add(n+i, n+i+, c);///层与层之间连边;
Add(n+i+, n+i, c);
}
}
for(int i=; i<=n; i++)
{
Add(n+l[i], i, );///当前点与它所在的层连边,边为0;
if(l[i]!=) Add(i, n+l[i]-, c);///点和层间进行连边;
if(l[i]!=n) Add(i, n+l[i]+, c);
} for(int i=; i<=m; i++)
{
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
Add(u, v, w);
Add(v, u, w);
}
int ans = spfa();
if(ans == INF) ans = -;
printf("Case #%d: %d\n", t++, ans);
}
return ;
}

The Shortest Path in Nya Graph---hdu4725(spfa+扩点建图)的更多相关文章

  1. HDU 4725 The Shortest Path in Nya Graph(spfa+虚拟点建图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题目大意:有n层,n个点分布在这些层上,相邻层的点是可以联通的且距离为c,还有额外给出了m个条边 ...

  2. ACM学习历程—HDU4725 The Shortest Path in Nya Graph(SPFA && 优先队列)

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

  3. 2013成都邀请赛J称号||HDU4725 The Shortest Path in Nya Graph(spfa+slf最短的优化)

    职务地址:HDU 4725 这题卡了好长时间了,建图倒是会建,可是不会最短路的算法优化,本以为都须要堆去优化的,打算学了堆之后再来优化.可是昨晚CF的一道题..(那题也是不优化过不了..)然后我就知道 ...

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

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

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

  6. 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 题意: 在一个图中跑最 ...

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

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

随机推荐

  1. setTimeout()和setInterval() 何时被调用执行

    定义 setTimeout()和setInterval()经常被用来处理延时和定时任务.setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式,而setInterval()则可以在每隔 ...

  2. 有关g++编译调试的问题

    打了个指针版的treap,想用gdb调试,用gcc -g ×××.cpp -o a 时却报错了——直接用gcc编译却不会报错,提示:对‘operator new(unsigned int)’未定义的引 ...

  3. oracle中用SQL实现两个日期间的日期形成一个数据集

    比如输入2014-06-1 和 2014-07-1形成一个2014-06-1 2014-06-22014-06-3...2014-07-1 的数据集.   解决方法: select date'2014 ...

  4. DELPHI的split函数的各种实现方法(转)

    一.单字符 function split(s,s1:string):TStringList;beginResult:=TStringList.Create;while Pos(s1,s)>0 d ...

  5. iOS开发项目之一 [ 项目流程]

    项目流程 *人员配置 *客户端(iOS工程师,Android工程师) *前端 h5 *后台人员(php,java,net) *提供接口(请求地址.请求参数,请求方式,接口文档) *UI UE * 效果 ...

  6. csv格式

    Name,Password nmae:xiaofan,password:1234567890 每个逗号就是一列

  7. 我装GitHub的过程

    GitHub是老师推荐的没正真的使用过,这次安装也是按提示的,不知对否,且还没使用,只是记录一下自己的过程.我是在线安装的. 1.下载GitHub安装问价,双击开始安装 2.出现的可能是系统相关配置吧 ...

  8. 初探Bootstrap之十二栅格

    模型1: <!-- 4/12 --> <div class="container"> <div class="row"> & ...

  9. HDU 1003 基础dp 最大连续序列和

    常常做错的一道题.. 因为总是要有一个长度的 所以一开始的s与e都是1 maxx也是a[1] 然后再求 从i=2开始 在这里注意 me永远是当前i 而ms则可能留在原地 可能直接等于i 判断条件就是当 ...

  10. Apache Spark源码走读之18 -- 使用Intellij idea调试Spark源码

    欢迎转载,转载请注明出处,徽沪一郎. 概要 上篇博文讲述了如何通过修改源码来查看调用堆栈,尽管也很实用,但每修改一次都需要编译,花费的时间不少,效率不高,而且属于侵入性的修改,不优雅.本篇讲述如何使用 ...