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. javaweb中的三个域

    1.Request域 程序产生数据,显示完了就没用了,就用这个域. 2.Session域 程序产生数据,出了显示用,待会还要用,就用这个域. 3.ServletContext域 程序产生数据,数据显示 ...

  2. codevs1959拔河比赛(二维费用背包)

    1959 拔河比赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold     题目描述 Description 一个学校举行拔河比赛,所有的人被分成了两组,每个人 ...

  3. C# 导出word 表格代码

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  4. 如何通过免费开源ERP Odoo实现企业数字化转型深度分析(一)

    本文来自<开源智造企业数字化转型报告白皮书>的精选内容章节.请勿转载.欢迎您反馈阅读意见. 引言 在由消费者驱动的数字经济时代,创新之势锐不可挡.变革步伐从未如此迅速,并且还会越来越快.对 ...

  5. ACM_魔仙岛探险(深搜)

    魔仙岛探险 Time Limit: 2000/1000ms (Java/Others) Problem Description: 小敏通过秘密方法得到一张不完整的魔仙岛航拍地图.魔仙岛由一个主岛和一些 ...

  6. 猜拳游戏项目(涉及知识点Scanner、Random、For、数组、Break、Continue等)

    package day03.d3.xunhuankongzhi; import java.util.Scanner; public class CaiQuan { public static void ...

  7. RabbitMQ 官方NET教程(六)【RPC】

    在第二个教程中,我们学习了如何使用Work Queues在多个工作者之间分配耗时的任务. 但是如果我们需要在远程计算机上运行功能并等待结果怎么办? 那是一个不同的模式. 此模式通常称为远程过程调用或R ...

  8. SQLServer In和Exists

    In          Exists () 1分42秒  5秒 Exists() 返回布尔值 如果子查询结果行>0,则返回 TRUE. 反之返回FALSE exists(select * fro ...

  9. python--1、入门

    python的创始人为吉多·范罗苏姆(Guido van Rossum). python在2017年统计的所有语言排名中处于第四名,稳步上升状态. python应用领域: WEB开发(Django框架 ...

  10. MySQL笔试题搜罗

    一.有表如下 +------+---------+--------+ | name | subject | score | +------+---------+--------+ | 张三 | 数学 ...