Seaside HDU 3665 【Dijkstra】

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();
	}
}
时间: 2024-10-21 11:57:08

Seaside HDU 3665 【Dijkstra】的相关文章

HDU2112 HDU Today 【Dijkstra】

HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14743    Accepted Submission(s): 3471 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候

HDU 2112:HDU Today【Dijkstra】

HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 19635    Accepted Submission(s): 4615 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候

HDOJ 1874 畅通工程续 【dijkstra】

题意:... 策略:最简单的求最短路径. 代码: #include<stdio.h> #include<string.h> #define MAXN 1005 #define INF 0x3f3f3f3f int di[MAXN], vis[MAXN], n, m; int map[MAXN][MAXN]; void dijkstra(int v) { int i, j; memset(vis, 0, sizeof(vis)); di[v] = 0; vis[v] = 1; for

HDU2680 Choose the best route 【Dijkstra】

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7061    Accepted Submission(s): 2300 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

HDU1874 畅通工程续 【Dijkstra】

畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 26970    Accepted Submission(s): 9719 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走

HDU2544 最短路 【Dijkstra】

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 31182    Accepted Submission(s): 13456 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

HDOJ 1142 A Walk Through the Forest 【Dijkstra】+【DFS】

题意:从2到1的所有路径中找出最短的路,并且输出最短路径有几条. 策略:先求出最短路径,然后再找出从2到1的最短路径有几条.最短路径用dijkstra算法来求出,什么是dijkstra算法,简单来说,dijkstra算法就是路径长度递增次序产生最短路径的算法: 基本思想是:把集合V分成两组: (1)S:已求出最短路径的顶点的集合 (2)V-S=T:尚未确定最短路径的顶点集合 将T中顶点按最短路径递增的次序加入到S中, 保证:(1)从源点V0到S中各顶点的最短路径长度都不大于 从V0到T中任何顶点

HDU3790 最短路径问题 【Dijkstra】

最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13336    Accepted Submission(s): 4072 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. Input

ZOJ--1655--Transport Goods【dijkstra】

题意:某国首都正被攻打,需要运送物资到首都,告诉你n个点,编号1~n,n是首都,剩下的点各有wi重量的物资,m条路,每条路有个货物损失比例,现需要求出最多能运送多少货物到首都. 其实转换一下就是一个最短路问题,边的权值是损失比例,找损失比例最小的那条路,则能运送的货物最多. dist数组存放运成功的比例,初始化为0表示运不成. WA了N发,各种double类型都用int定义的,而且它给的样例即使定义成int对结果也没影响... #include<cstring> #include<str