您的位置:首页 > 其它

UVa10816 - Travel in Desert(二分法+最短路径求法)

2014-10-29 18:55 316 查看
There is a group of adventurers who like to travel in the desert. Everyone knows travelling in desert can be very dangerous. That's why they plan their trip carefully every time. There are a lot of factors to consider
before they make their final decision.

One of the most important factors is the weather. It is undesirable to travel under extremely high temperature. They always try to avoid going to the hottest place. However, it is unavoidable sometimes as it might be on the only way to the destination. To
decide where to go, they will pick a route that the highest temperature is minimized. If more than one route satisfy this criterion, they will choose the shortest one.

There are several oases in the desert where they can take a rest. That means they are travelling from oasis to oasis before reaching the destination. They know the lengths and the temperatures of the paths between oases. You are to
write a program and plan the route for them.

Input

Input consists of several test cases. Your program must process all of them.

The first line contains two integers N and E (1 ≤ N ≤ 100; 1 ≤
E ≤ 10000) where N represents the number of oasis and E represents the number of paths between them. Next line contains two distinct integers
S and T (1 ≤ S, T ≤ N) representing the starting point and the destination respectively. The following
E lines are the information the group gathered. Each line contains 2 integers
X, Y and 2 real numbers R and D (1 ≤ X, Y ≤
N; 20 ≤ R ≤ 50; 0 < D ≤ 40). It means there is a path between
X and Y, with length D km and highest temperature
R oC. Each real number has exactly one digit after the decimal point. There might be more than one path between a pair of oases.

Output

Print two lines for each test case. The first line should give the route you find, and the second should contain its length and maximum temperature.

Sample Input

6 9
1 6
1 2 37.1 10.2
2 3 40.5 20.7
3 4 42.8 19.0
3 1 38.3 15.8
4 5 39.7 11.1
6 3 36.0 22.5
5 6 43.9 10.2
2 6 44.2 15.2
4 6 34.2 17.4


Sample Output

1 3 6
38.3 38.3

题意 :求出路途中温度最大值最小化的最短路径
思路:用二分法+最短路径的法(Dijkstra或者SPFA)

注意用二分法时,当最大值r与最小值l的差大于1e-8时继续,最后一次求最短路径时用r,不能用l和(r +l)/2

Dijkstra法代码如下:

#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <limits>
#include <cstring>
#include <cassert>

using namespace std;

const int MAXN = 110;
const double EPS = 1e-8;
const int INF = 0x3f3f3f3f;

struct Edge
{
int from, to;
double r, d;
};

struct Node
{
int v;
double d;

bool operator < (const Node &other) const
{
return d > other.d;
}
};

vector<int> adjList[MAXN];
vector<Edge> edges;
int n, e;
int src, dest;
double minTemp, maxTemp;
bool vis[MAXN];
double d[MAXN];
int p[MAXN];

void addEdge(int u, int v, double r, double d)
{
edges.push_back((Edge){u, v, r, d});
edges.push_back((Edge){v, u, r, d});

int size = edges.size();
adjList[u].push_back(size - 2);
adjList[v].push_back(size - 1);
}

bool input()
{
if (scanf("%d%d", &n, &e) != 2) return false;

scanf("%d%d", &src, &dest);

minTemp = INF;
maxTemp = 0;

for (int i = 1; i <= n; i++) adjList[i].clear();
edges.clear();

for (int i = 0; i < e; i++) {
int u, v;
double r, d;
scanf("%d%d%lf%lf", &u, &v, &r, &d);
assert(u != 0);
assert(v != 0);
addEdge(u, v, r, d);
minTemp = min(r, minTemp);
maxTemp = max(r, maxTemp);
}

return true;
}

bool dijkstra(double mid)
{
priority_queue<Node> q;

memset(vis, false, sizeof(vis));
memset(p, 0x00, sizeof(p));

for (int i = 1; i <= n; i++) {
d[i] = INF;
}

d[src] = 0;

q.push((Node){src, 0});

while (!q.empty()) {
Node cur = q.top(); q.pop();

if (vis[cur.v]) continue;
vis[cur.v] = true;

if (cur.v == dest) {
return true;
}

for (int i = 0, size = adjList[cur.v].size(); i < size; i++) {
Edge& e = edges[adjList[cur.v][i]];
//printf("e.r=%.8f, mid=%.8f, %d\n", e.r, mid, e.r <= mid);
if (e.r < mid + EPS) {
//printf("d[e.from] + e.d=%.8f, d[e.to] - EPS=%.8f, %d\n", d[e.from] + e.d , d[e.to] - EPS, d[e.from] + e.d < d[e.to] - EPS);
if (d[e.from] + e.d < d[e.to] - EPS) {
d[e.to] = d[e.from] + e.d;
q.push((Node){e.to, d[e.to]});
p[e.to] = e.from;
}
}
}
}

return false;
}

void print(int s, int t)
{
if (t == s) {
printf("%d", s);
return;
}

print(s, p[t]);
printf(" %d", t);
}

void solve()
{
double l = minTemp, r = maxTemp;
double mid;

//printf("minTemp=%.1lf, maxTemp=%.1lf\n", l, r);
while (r - l > EPS) {
mid = (r + l) / 2;
//printf("%.8f, %d\n", mid, dijkstra(mid));
if (dijkstra(mid)) {
r = mid;
} else {
l = mid;
}
}

dijkstra(r);

int ans[MAXN];
int cnt = 0;
int t = dest;

while (t != 0) {
ans[cnt++] = t;
t = p[t];
}

for (int i = cnt - 1; i >= 0; i--) {
if (i != cnt - 1) printf(" ");
printf("%d", ans[i]);
}

//print(src, dest);
printf("\n%.1f %.1f\n", d[dest], r);
}

int main(int argc, char **argv)
{
#ifndef ONLINE_JUDGE
freopen("e:\\uva_in.txt", "r", stdin);
//freopen("e:\\uva_out.txt", "w", stdout);
#endif

while (input()) {
solve();
}

return 0;
}


SPFA法代码如下:
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>

using namespace std;

const int MAXN = 110;
const double EPS = 1e-8;
const int INF = 0x3f3f3f3f;

struct Edge
{
int from, to;
double r, d;
};

vector<int> adjList[MAXN];
vector<Edge> edges;
int n, e;
int src, dest;
double minTemp, maxTemp;
bool inq[MAXN];
double d[MAXN];
int p[MAXN];

void addEdge(int u, int v, double r, double d)
{
edges.push_back((Edge){u, v, r, d});
edges.push_back((Edge){v, u, r, d});

int size = edges.size();
adjList[u].push_back(size - 2);
adjList[v].push_back(size - 1);
}

bool input()
{
if (scanf("%d%d", &n, &e) != 2) return false;

scanf("%d%d", &src, &dest);

minTemp = INF;
maxTemp = 0;

for (int i = 1; i <= n; i++) adjList[i].clear();
edges.clear();

for (int i = 0; i < e; i++) {
int u, v;
double r, d;
scanf("%d%d%lf%lf", &u, &v, &r, &d);
addEdge(u, v, r, d);
minTemp = min(r, minTemp);
maxTemp = max(r, maxTemp);
}

return true;
}

bool spfa(double mid)
{
queue<int> q;

memset(inq, false, sizeof(inq));
memset(p, 0x00, sizeof(p));

for (int i = 1; i <= n; i++) {
d[i] = INF;
}

d[src] = 0;

q.push(src);
inq[src] = true;
while (!q.empty()) {
int u = q.front(); q.pop();
if (u == dest) return true;

inq[u] = false;
for (int i = 0, size = adjList[u].size(); i < size; i++) {
Edge& e = edges[adjList[u][i]];
if (e.r < mid + EPS) {
if (d[e.from] + e.d < d[e.to] - EPS) {
d[e.to] = d[e.from] + e.d;
p[e.to] = e.from;
if (!inq[e.to]) {
q.push(e.to);
inq[e.to] = true;
}
}
}
}
}

return false;
}

void solve()
{
double l = minTemp, r = maxTemp;
double mid;

//printf("minTemp=%.1lf, maxTemp=%.1lf\n", l, r);
while (r - l > EPS) {
mid = (r + l) / 2;
//printf("%.1lf, %d\n", mid, dijkstra(mid));
if (spfa(mid)) {
r = mid;
} else {
l = mid;
}
}

spfa(r);
int ans[MAXN];
int cnt = 0;
int t = dest;

while (t != 0) {
ans[cnt++] = t;
t = p[t];
}

for (int i = cnt - 1; i >= 0; i--) {
if (i != cnt - 1) printf(" ");
printf("%d", ans[i]);
}
printf("\n%.1f %.1f\n", d[dest], r);
}

int main(int argc, char **argv)
{
#ifndef ONLINE_JUDGE
freopen("e:\\uva_in.txt", "r", stdin);
#endif

while (input()) {
solve();
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: