Hdu1598 find the most comfortable road [query set] + [enumeration]

Source: Internet
Author: User

Find the most comfortable roadtime limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others) Total submission (s): 3686 accepted submission (s): 1565

Problem descriptionxx has many cities that communicate with each other through a strange highway SARS (super air roam structure --- super Air Roaming structure, each piece of SARS imposes a fixed speed limit on the flycar driving. Meanwhile, XX has special requirements on flycar's "comfort, that is, the lower the speed difference between the highest speed and the lowest speed, the more comfortable the ride. (It is understood that flycar must speed up/speed down instantly, which is painful ),
However, XX has less requirements on time. You need to find the most comfortable path between cities. (SARS is bidirectional ).
 
Input includes multiple test instances, each of which includes:
The first line has two positive integers n (1 <n <= 200) and M (M <= 1000), indicating that there are n cities and m sars.
The following rows are three positive integers, startcity, endcity, and speed. startcity is converted to endcity on the surface, and the speed limit is speedsars. Speed <= 1000000
Then there is a positive integer Q (q <11), indicating the number of pathfinding.
Next, each row in row Q has two positive integers start and end, indicating the start and end of the path.
Output: print a line for each route seeking. Only one non-negative integer is output, indicating the maximum comfort speed and the lowest speed of the optimal route. If the start and end cannot be reached, the output is-1.
Sample Input
4 41 2 22 3 41 4 13 4 221 31 2
 
Sample output
10
Question: The minimum speed difference in the China Unicom Road.

Question: first sort edge weights from small to large, and then query the set enumeration.

#include <stdio.h>#include <string.h>#include <limits.h>#include <algorithm>#define maxn 202#define maxm 1002using std::sort;using std::min;int pre[maxn];struct Node{int u, v, cost;} E[maxm];int ufind(int k){int a = k, b;while(pre[k] != -1) k = pre[k];while(a != k){b = pre[a];pre[a] = k;a = b;}return k;}bool cmp(Node a, Node b){return a.cost < b.cost;}int main(){int n, m, q, a, b, i, j, x, y, ans;while(scanf("%d%d", &n, &m) == 2){for(i = 0; i < m; ++i)scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].cost);sort(E, E + m, cmp);scanf("%d", &q);while(q--){scanf("%d%d", &a, &b);ans = INT_MAX;for(i = 0; i < m; ++i){memset(pre, -1, sizeof(pre));for(j = i; j < m; ++j){x = ufind(E[j].u);y = ufind(E[j].v);if(x != y) pre[x] = y;if(ufind(a) == ufind(b)){ans = min(ans, E[j].cost - E[i].cost);break;}}}if(ans == INT_MAX) printf("-1\n");else printf("%d\n", ans);}}return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.