URAL 1119 Metro (DP动态规划)

#include <stdio.h>
#define MAX 1000
#define INF 1e6
#define MIN(x, y) ( (x) < (y) ? (x) : (y) )
int WEsize, SNsize;
int numOfCrosses;
int x, y;
float shortestRoute[MAX + 1][MAX + 1];

int main(){

	scanf("%d%d", &WEsize, &SNsize);
	scanf("%d", &numOfCrosses);
	int cross;
	for (cross = 0; cross < numOfCrosses; cross++){
		scanf("%d %d", &x, &y);
		shortestRoute[x][y] = -1;
	}

	for (x = 0; x <= WEsize; x++)
		shortestRoute[x][0] = x * 100;
	for (y = 0; y <= SNsize; y++)
		shortestRoute[0][y] = y * 100;
	shortestRoute[0][0] = 0.0;

	for (x = 1; x  <= WEsize; x++)
		for (y = 1; y <= SNsize; y++){
			float temp = MIN(shortestRoute[x][y - 1], shortestRoute[x - 1][y]) + 100;
			if (shortestRoute[x][y] == -1)
				shortestRoute[x][y] = MIN(temp, shortestRoute[x - 1][y - 1] + 141.42);
			else 
				shortestRoute[x][y] = temp;
		}

	printf("%.0f\n", shortestRoute[WEsize][SNsize]);

	return 0;
}

你可能感兴趣的:(dp,动态规划,metro,1119,ural)