首页 > ACM题库 > HDU-杭电 > HDU 3665-Seaside-最短路径-[解题报告]HOJ
2014
11-30

HDU 3665-Seaside-最短路径-[解题报告]HOJ

Seaside

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

5
1 0
1 1
2 0
2 3
3 1
1 1
4 100
0 1
0 1

样例输出:

2

最短路水题啊 = =。。。临时输出项忘删了,WA了一次 = = 。。。

这也算区域赛题目啊。。。这么裸的最短路。。我用的dijkstra,应该用Floyd更划算吧。。。数据这么小。。

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define BUG puts("here!!!")
#define STOP system("pause")

using namespace std;

const int MAX = 15;
int mm[MAX][MAX];
bool sea[MAX];

void init()
{
	memset(sea, false, sizeof(sea));
	for(int i=0; i<MAX; i++)
	{
		for(int k=0; k<MAX; k++)
			mm[i][k] = INT_MAX;
		mm[i][i] = 0;
	}
}

int Dijkstra(int from, int n)
{
	int dis[MAX];
	bool used[MAX];
	for(int i=0; i<n; i++) dis[i] = INT_MAX;
	memset(used, false, sizeof(used));
	used[from] = true; dis[from] = 0;
	int now = from;
	for(int i=0; i<n; i++)
	{
		for(int k=0; k<n; k++)
			if( mm[now][k] != INT_MAX && dis[now] != INT_MAX 
				&& mm[now][k] + dis[now] < dis[k] )
				dis[k] = dis[now] + mm[now][k];
		
		int mmin = INT_MAX;
		
		for(int k=0; k<n; k++)
			if( mmin > dis[k] && !used[k] )
				mmin = dis[now = k];
		used[now] = true;
	}
	
	int ans = INT_MAX;
	for(int i=0; i<n; i++)
		if( sea[i] && ans > dis[i] )
			ans = dis[i];
	return ans;
}

int main()
{
	int n, p, m, to, len;
	
	while( ~scanf("%d", &n) )
	{
		init();		
		for(int i=0; i<n; i++)
		{
			scanf("%d%d",&m, &p);
			if( p ) sea[i] = true;
			while( m-- )
			{
				scanf("%d%d", &to, &len);
				if( len < mm[i][to] )
					mm[i][to] = mm[to][i] = len;
			}
		}
		
		int ans = Dijkstra(0, n);
		
		printf("%d\n", ans);
	}

return 0;
}

参考:http://blog.csdn.net/zxy_snow/article/details/6713596


  1. 您没有考虑 树的根节点是负数的情况, 若树的根节点是个很大的负数,那么就要考虑过不过另外一边子树了

  2. #include <cstdio>

    int main() {
    //answer must be odd
    int n, u, d;
    while(scanf("%d%d%d",&n,&u,&d)==3 && n>0) {
    if(n<=u) { puts("1"); continue; }
    n-=u; u-=d; n+=u-1; n/=u;
    n<<=1, ++n;
    printf("%dn",n);
    }
    return 0;
    }

  3. Thanks for taking the time to examine this, I really feel strongly about it and love studying a lot more on this topic. If possible, as you acquire experience