URAL 2069 Hard Rock (最短路)

题意:给定 n + m 个街道,问你从左上角走到右下角的所有路的权值最小的中的最大的。

析:我们只要考虑几种情况就好了,先走行再走列和先走列再走行差不多。要么是先横着,再竖着,要么是先横再竖再横,要么是先横再竖再横再竖,全考虑一下就好了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int row[maxn], col[maxn]; int solve(int *a, int r, int *b, int c){
int ans1 = min(a[0], b[c]);
int ans2 = min(a[0], a[r]);
int cnt = 0;
for(int i = 0; i <= c; ++i)
cnt = max(cnt, b[i]);
ans2 = min(ans2, cnt);
int ans3 = min(a[0], b[c]);
int rr = 0;
for(int i = 0; i <= r; ++i)
rr = max(rr, a[i]);
ans2 = min(ans2, min(cnt, rr));
return max(ans1, max(ans2, ans3));
} int main(){
while(scanf("%d %d", &m, &n) == 2){
for(int i = 0; i < m; ++i)
scanf("%d", &col[i]);
for(int j = 0; j < n; ++j)
scanf("%d", &row[j]);
int ans = solve(row, n-1, col, m-1);
ans = max(ans, solve(col, m-1, row, n-1));
printf("%d\n", ans);
}
return 0;
}

  

上一篇:Javascript高级程序设计第三版-笔记


下一篇:java集合类(二)