Problem Description
XiaoY is living in a big city, there are N towns in it and some towns near the sea. All these towns are numbered from 0 to N-1 and XiaoY lives in the town numbered ’0’. There are some directed roads connecting them. It is guaranteed that you can reach any town from the town numbered ’0’, but not all towns connect to each other by roads directly, and there is no ring in this city. One day, XiaoY want to go to the seaside, he asks you to help him find out the shortest way.
 
Input
There are several test cases. In each cases the first line contains an integer N (0<=N<=10), indicating the number of the towns. Then followed N blocks of data, in block-i there are two integers, Mi (0<=Mi<=N-1) and Pi, then Mi lines followed. Mi means there are Mi roads beginning with the i-th town. Pi indicates whether the i-th town is near to the sea, Pi=0 means No, Pi=1 means Yes. In next Mi lines, each line contains two integers SMi and LMi, which means that the distance between the i-th town and the SMi town is LMi.
 
Output
Each case takes one line, print the shortest length that XiaoY reach seaside.
 
Sample Input
5 1 0 1 1 2 0 2 3 3 1 1 1 4 100 0 1 0 1
 
Sample Output
2

思路:
在Dijkstra的基础上稍作修改即可,注意0x7fffffff可能会超范围

#include <iostream>
#include <cstdio>
#include <cstring>
#define INF 0x7ffffff
using namespace std; int N;
int G[][];
int tmp;
int sea[];
int a,b;
int s[];
int d[];
int minn;
int v;
int ans;
int st[]; int min(int a,int b)
{
return a<b?a:b;
} int main()
{
while(~scanf("%d",&N))
{
if(N == ){
printf("0\n");
continue;
}
ans = INF;
memset(s,,sizeof(s));
for(int i = ;i < N;i++)
for(int j = ;j < N;j++)
G[i][j] = i==j?:INF;
for(int i = ;i < N;i++) {
scanf("%d%d",&tmp,&sea[i]);
while(tmp--) {
scanf("%d%d",&a,&b);
G[i][a] = b;
}
}
if(sea[]){
printf("0\n");
continue;
}
s[] = ;
for(int i = ;i < N;i++)
d[i] = G[][i];
for(int i = ;i < N;i++)
{
minn = INF;
for(int j = ;j < N;j++)
if(!s[j] && d[j]<minn) minn = d[v=j];
s[v] = ;
if(sea[v]) ans = min(ans,minn);
for(int j = ;j < N;j++)
if(!s[j] && d[j]>G[v][j]+minn) d[j] = G[v][j]+minn;
}
printf("%d\n",ans);
}
return ;
}

HDU-3665(单源最短路)的更多相关文章

  1. HDU 2544 单源最短路

    题目链接: 传送门 最短路 Time Limit: 1000MS     Memory Limit: 65536K 题目描述 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是 ...

  2. [模板][HDU]P2544[单源最短路][SPFA]

    题目就不放了,主要是写一下SPFA,很少写,今天特别学了一个用STL的队列来做的. 代码: #include<iostream> #include<cstdio> #inclu ...

  3. 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)

    关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...

  4. [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  5. 用scheme语言实现SPFA算法(单源最短路)

    最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学 ...

  6. 单源最短路_SPFA_C++

    当我们需要求一个点到其它所有点的最短路时,我们可以采用SPFA算法 代码特别好写,而且可以有环,但是不能有负权环,时间复杂度是O(α(n)n),n为边数,α(n)为n的反阿克曼函数,一般小于等于4 模 ...

  7. 【UVA1416】(LA4080) Warfare And Logistics (单源最短路)

    题目: Sample Input4 6 10001 3 21 4 42 1 32 3 33 4 14 2 2Sample Output28 38 题意: 给出n个节点m条无向边的图,每条边权都为正.令 ...

  8. 【算法系列学习】Dijkstra单源最短路 [kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home

    https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 ...

  9. 模板C++ 03图论算法 1最短路之单源最短路(SPFA)

    3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...

  10. 2018/1/28 每日一学 单源最短路的SPFA算法以及其他三大最短路算法比较总结

    刚刚AC的pj普及组第四题就是一种单源最短路. 我们知道当一个图存在负权边时像Dijkstra等算法便无法实现: 而Bellman-Ford算法的复杂度又过高O(V*E),SPFA算法便派上用场了. ...

随机推荐

  1. Android-语言设置流程分析

    Android手机语言切换行为,是通过设置-语言和输入法-语言来改变手机的语言,其实这个功能很少被用户使用.     以Android5.1工程源码为基础,从设置app入手来分析和学习语言切换的过程: ...

  2. SWFObject文件上传使用记录

    SWFObject文件上传使用方法记录,该插件使用起来相当强大也很灵活,与uploadify各有千秋. 值得一说的是,如果要设置button_image_url这个参数,该参数是按钮的背景图,但是一定 ...

  3. C# string.Format谨慎使用

    string.Format string.Format在处理文本的时候很有用处,但是在使用占位符的时候一定要注意内容中的特殊字符{}. 示例 string.Format("你好{0},这是{ ...

  4. JavaScript 总结

    1. JavaScript prototype属性是一个对象 当一个函数在定义之后 就会自动获得这个属性.其初始值是一个空对象.新建了一个名为Cat的构造函数,其prototype为一个对象,cons ...

  5. H1标签使用的七大注意事项

    H1标签使用的七大注意事项: 1 每个页面都应该有H1标签,H1标签是每个网页不可缺少的要素. 2 使用H1标签的内容应该简洁明了; 3 H1标签要尽量出现在源文件代码的开头的部分,这样可以让访问者和 ...

  6. MongoDB与PHP的添加、修改、查询、删除

    链接数据库使用下面的代码创建一个数据库链接 <?php$connection = new Mongo(); //链接到 localhost:27017$connection = new Mong ...

  7. (转)CentOS下用yum搭建LNMP服务器

    原文链接:http://www.xiaohuai.com/2733 CentOS下搭服务器也折腾好几次了, 每次都知道个大概, 具体repo的地址什么的还都要现找, 实在不效率, 干脆整理记录下来. ...

  8. Arcgis Android 基本概念 - 浅谈

    MapView     MapView 是 Android 中 ViewGroup的子类,也是 ArcGIS Runtime SDK for Android 中的地图容器,与很多 ArcGIS API ...

  9. Windows下配置环境变量和需不需要重启问题

    http://blog.163.com/guomaolin_gavin/blog/static/19961830720121114929321/

  10. PL/SQL中的变量案例解析

    1.标量: ag1: declare v_ename emp.ename%type;--自己称为单变量 begin select ename into v_ename from emp where e ...