2014
11-30

# 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

#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;
}


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

2. #include <cstdio>

int main() {