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

#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
int N;
int n;
int low[20];
bool vis[20];
int map[20][20];
void Dijkstra()
{
int pos=0;
int i,j;
memset(vis,0,sizeof(vis));
memset(low,INF,sizeof(low));
for(i=0;i<n;++i)
{
low[i]=map[0][i];
}
vis[0]=1;
low[0]=0;
for(i=0;i<n-1;++i)
{
int min=INF;
for(j=0;j<n;++j)
{
if(!vis[j]&&min>low[j])
{
min=low[j];
pos=j;
}
}
vis[pos]=1;
for(j=0;j<n;++j)
{
if(!vis[j]&&low[j]>map[pos][j]+low[pos])
low[j]=map[pos][j]+low[pos];
}
}
printf("%d\n",low[n-1]);
}
int main()
{
int i, j;
int M, flag;
int u,v,w;
while(~scanf("%d", &N))
{
n=N+1;
for(i=0;i<=N;++i)
for(j=0;j<=i;++j)
map[i][j]=map[j][i]=INF;
//memset(map,INF,sizeof(map));
for(int i = 0; i < N; ++i)
{
scanf("%d%d", &M, &flag);
if(flag) map[i][n-1]=map[n-1][i]=0;
while(M--)
{
scanf("%d%d",&v,&w);
if(map[i][v]>w)
map[i][v]=map[v][i]=w;
}
}
Dijkstra();
}
}

Seaside HDU 3665 【Dijkstra】的更多相关文章

  1. 【Dijkstra】

    [摘自]:华山大师兄,推荐他的过程动画~   myth_HG 定义 Dijkstra算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩 ...

  2. How far away ? HDU - 2586 【LCA】【RMQ】【java】

    题目大意:求树上任意两点距离. 思路: dis[i]表示i到根的距离(手动选根),则u.v的距离=dis[u]+dis[v]-2*dis[lca(u,v)]. lca:u~v的dfs序列区间里,深度最 ...

  3. HDU 3485【101】 51nod 1668【010】 joj 2171【111】动态规划

    有一个只含0和1的长度为n的串,问不含有101的所有串的个数. ——不存在连续的101.010.111的字符串数量 HDU:https://cn.vjudge.net/problem/HDU-3485 ...

  4. HDU 3790 最短路径问题【Dijkstra】

    题意:给出n个点,m条边,每条边的长度d和花费p,给出起点和终点的最短距离和花费,求最短距离,如果有多个最短距离,输出花费最少的 在用dijkstra求最短距离的时候,再用一个f[]数组保存下最少花费 ...

  5. HDU 2066 一个人的旅行【Dijkstra 】

    题意:给出s个起点,d个终点,问从这些起点到达终点的最短距离 因为有多个起点,所以把这多个起点的值设为0 哎= =改了好久的说= = 是因为在代码里面的t,不知道为什么调用dijkstra()函数之后 ...

  6. HDU 4370 0 or 1 (01规划)【Dijkstra】||【spfa】

    <题目链接> 题目大意: 一个n*n的01矩阵,满足以下条件 1.X12+X13+...X1n=12.X1n+X2n+...Xn-1n=13.for each i (1<i<n ...

  7. hdu 3499 flight 【分层图】+【Dijkstra】

    <题目链接> 题目大意: 现在给你一些点,这些点之间存在一些有向边,每条边都有对应的边权,有一次机会能够使某条边的边权变为原来的1/2,求从起点到终点的最短距离. 解题分析: 分层图最短路 ...

  8. POJ1797 Heavy Transportation 【Dijkstra】

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 21037   Accepted:  ...

  9. poj 2502 Subway【Dijkstra】

    <题目链接> 题目大意: 某学生从家到学校之间有N(<200)条地铁,这个学生可以在任意站点上下车,无论何时都能赶上地铁,可以从一条地铁的任意一站到另一条地跌的任意一站,学生步行速度 ...

随机推荐

  1. bzoj 1556 墓地秘密 —— 状压DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1556 预处理出两个障碍四个方向之间的距离(转弯次数),就可以状压DP了: 但预处理很麻烦.. ...

  2. 第2章 安装Nodejs 2-3 Windows下安装Nodejs

    http://nodejs.org

  3. bzoj 2152 聪聪可可(点分治模板)

    2152: 聪聪可可 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 3194  Solved: 1647[Submit][Status][Discuss ...

  4. html5 窗口之间的通信

    一般窗口通信分为三种: iframe嵌套:多个iframe之间通信. 父页面操作子页面元素:oFrame.contentWindow.document.body. 父页面调用子页面方法:oFrame. ...

  5. Python 40 数据库-约束

    约束 1.什么是约束 ? 除了数据类型以外额外添加的约束 2.为什么要使用约束 ? 为了保证数据的合法性 完整性 分类: 1.not null 非空约束,数据不能为空----------------- ...

  6. springboot配置过滤器和拦截器

    import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Http ...

  7. Python描述符:property()函数的小秘密

    描述符:将某种特殊类型的类的实例指派给另一个类的属性(注意:这里是类属性,而不是对象属性).而这种特殊类型的类就是实现了__get__,__set__,__delete__这三个方法中的一个或多个的新 ...

  8. xhtml1-frameset.dtd

    <!-- Extensible HTML version 1.0 Frameset DTD This is the same as HTML 4 Frameset except for chan ...

  9. PHP开发笔记(三)关于PHP伪静态的问题总结

    Apache 第一个问题就是关于PHPStudy集成Apache环境下5.5版本以上”No input file specified“问题. 针对TP5框架,以下是.htaccess文件的配置,PHP ...

  10. (转)vuex2.0 基本使用(2) --- mutation 和 action

    我们的项目非常简单,当点击+1按钮的时候,count 加1,点击-1按钮的时候,count 减1. 1, mutation The only way to actually change state ...