uva 10816 最短路 tle
Problem C: Travel in Desert |
Time limit: 5 seconds |
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
Problemsetter : Raymond Chun tle了,按照网上的解法:二分最高温度,dijkstra。
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<vector> #include<cstdlib> #include<algorithm> #include<queue> #include<map> #include<stack> using namespace std; #define LL long long #define ULL unsigned long long #define UINT unsigned int #define MAX_INT 0x7fffffff #define cint const int #define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) #define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) #define INF 100000000 #define MAXN 111 #define MAXM 21000 int n, m, s, t; struct edge{ int u, v; double r, d; int nxt; }e[MAXM]; int h[MAXN], cc; void add(int u, int v, double r, double d){ e[cc]=(edge){u, v, r, d, h[u]}; h[u]=cc++; e[cc]=(edge){v, u, r, d, h[v]}; h[v]=cc++; } struct node{ int u; double w; bool operator < (const node &rhs)const{ return w>rhs.w; } }; int p[MAXN]; double d[MAXN]; bool done[MAXN]; bool dijkstra(double thresh){ priority_queue<node> q; fill_n(done+1, n, false); fill_n(d+1, n, INF); d[s]=0; q.push((node){s, d[s]}); while(!q.empty()){ node ut = q.top(); q.pop(); int u = ut.u; if(u == t) break; if(done[u]) continue; done[u]=true; for(int i=h[u]; i!=-1; i=e[i].nxt){ int v=e[i].v; double w=e[i].d, r=e[i].r; if(r<=thresh && d[u]+w<d[v]){ d[v]=d[u]+w; p[v]=u; q.push((node){v, d[v]}); } } } return d[t]<INF; } void solve(double l, double r){ double temp, mid, dis; while(r-l>1e-3){ mid = (l + r) / 2; if(dijkstra(mid)) r = temp = mid, dis=d[t]; else l = mid; } stack<int> sta; for(int u=t; u!=s; u=p[u]) sta.push(u); printf("%d", s); while(!sta.empty()){ printf(" %d", sta.top()); sta.pop(); } printf("\n%.1f %.1f\n", dis, temp); } int main(){ // freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin); while(scanf(" %d %d %d %d", &n, &m, &s, &t)==4){ int i, x, y; double r, d; double minn=INF, maxx=0; fill_n(h+1, n, -1); cc=0; for(i=0; i<m; i++){ scanf(" %d %d %lf %lf", &x, &y, &r, &d); add(x, y, r, d); minn=MIN(minn, r); maxx=MAX(maxx, r); } solve(minn, maxx); } return 0; }