HDU 2363 Cycling(并查集 SPFA 暴力枚举)

原创 2016年08月30日 20:18:38

Cycling

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1286    Accepted Submission(s): 432


Problem Description
You want to cycle to a programming contest. The shortest route to the contest might be over the tops of some mountains and through some valleys. From past experience you know that you perform badly in programming contests after experiencing large differences in altitude. Therefore you decide to take the route that minimizes the altitude difference, where the altitude difference of a route is the difference between the maximum and the minimum height on the route. Your job is to write a program that finds this route.
You are given:

the number of crossings and their altitudes, and

the roads by which these crossings are connected.
Your program must find the route that minimizes the altitude difference between the highest and the lowest point on the route. If there are multiple possibilities, choose the shortest one.
For example:



In this case the shortest path from 1 to 7 would be through 2, 3 and 4, but the altitude difference of that path is 8. So, you prefer to go through 5, 6 and 4 for an altitude difference of 2. (Note that going from 6 directly to 7 directly would have the same difference in altitude, but the path would be longer!) 
 

Input
On the first line an integer t (1 <= t <= 100): the number of test cases. Then for each test case:

One line with two integers n (1 <= n <= 100) and m (0 <= m <= 5000): the number of crossings and the number of roads. The crossings are numbered 1..n.

n lines with one integer hi (0 <= hi <= 1 000 000 000): the altitude of the i-th crossing.

m lines with three integers aj , bj (1 <= aj , bj <= n) and cj (1 <= cj <= 1 000 000): this indicates that there is a two-way road between crossings aj and bj of length cj . You may assume that the altitude on a road between two crossings changes linearly.
You start at crossing 1 and the contest is at crossing n. It is guaranteed that it is possible to reach the programming contest from your home.
 

Output
For each testcase, output one line with two integers separated by a single space:

the minimum altitude difference, and

the length of shortest path with this altitude difference.
 

Sample Input
1 7 9 4 9 1 3 3 5 4 1 2 1 2 3 1 3 4 1 4 7 1 1 5 4 5 6 4 6 7 4 5 3 2 6 4 2
 

Sample Output
2 11
 

Source

题意:
找到最高值与最低值相差最少的路程并求出最短的路程。
思路:
参见HUD2337的做法。将海拔从低到高排序,然后用并查集枚举所有的可行解。在可行解中求出最短路。(注意开始点等于终止点的情况)。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#define MAXE 11000 
#define MAXV 110
#define INF 0X7fffffff
#define EINF 0x3f3f3f3f3f3f
using namespace std;

/****************************************************/
int marr[MAXV];
void reflash()    {for(int i = 0; i < MAXV; i++) marr[i] = i;}
int get(int num){while(marr[num] != num) num = marr[num];    return num;}
void merg(int a, int b){if(get(a) != get(b)) marr[get(a)] = get(b);}
/****************************************************/


struct node{
    int t, w, next;
}edge[MAXE];

int inde, head[MAXV], dist[MAXV];
bool oc[MAXV];

void addedge(int s, int t, int w){
    edge[inde].w = w;
    edge[inde].t = t;
    edge[inde].next = head[s];
    head[s] = inde++;
}

void fresh(){
    inde = 0;
    memset(head, -1, sizeof(head));
    memset(oc, 0, sizeof(oc));
}

struct node2{
    int num, h;
};
bool operator<(const node2& a, const node2& b){
    return a.h < b.h;
}
node2 point[MAXV];
bool occ[MAXV];

long long int spfa(int s, int t){
    int now, i;
    for(i = 0; i < MAXV; i++) dist[i] = INF;
    dist[s] = 0;
    queue<int> que;
    que.push(s);
    while(!que.empty()){
        now = que.front();
        que.pop();
        oc[now] = 0;
        i = head[now];
        while(i >= 0){
            if(!occ[edge[i].t]){
                i = edge[i].next;
                continue;
            }
            if(dist[edge[i].t] > dist[now] + edge[i].w){
                dist[edge[i].t] = dist[now] + edge[i].w;
                if(!oc[edge[i].t]){oc[edge[i].t] = 1; que.push(edge[i].t);}
            }
            i = edge[i].next;
        }
    }
    return dist[t];

}

struct node3{
    int s, t;
};

int main()
{
    int casen, num, edges, i, j,s, t, w ,k;
    long long int minh, minw;
    scanf("%d", &casen);
    while(casen--){
        minh = minw = EINF;
        fresh();
        scanf("%d%d", &num, &edges);
        for(i = 0; i < num; i++){
            scanf("%d", &point[i].h);
            point[i].num = i + 1;
        }
        sort(point, point + num);
        for(i = 0; i < edges; i++){
            scanf("%d%d%d", &s, &t, &w);
            addedge(s,t,w);
            addedge(t,s,w);
        }
        queue<node3> que;
        node3 temp;
        for(i = 0; i < num; i++){
            reflash();
            j = i;
            memset(occ, 0, sizeof(occ));
            while(j < num && get(1) != get(num)){
                occ[point[j].num] = 1;
                k = head[point[j].num];
                while(k >= 0){
                    if(occ[edge[k].t])  merg(point[j].num, edge[k].t);
                    k = edge[k].next;
                }
                j++;
                
            }
            if(get(1) == get(num)){
                j--;
                if(point[j].h - point[i].h < minh){
                    minh = point[j].h - point[i].h;
                    while(!que.empty()) que.pop();
                    temp.s = i; temp.t = j;
                    que.push(temp);
                }
                else if(point[j].h - point[i].h == minh){
                    temp.s = i; temp.t = j;
                    que.push(temp);
                }
            }
        }
        while(!que.empty()){
            temp = que.front();
            que.pop();
            memset(occ, 0, sizeof(occ));
            for(i = temp.s; i <= temp.t; i++){
                occ[point[i].num] = 1;
            }
            minw = min(minw, spfa(1, num));
        }
        if(1 == num)
            printf("0 0\n");
        else
            printf("%lld %lld\n",minh, minw);
        }
    return 0;
}



版权声明:From: http://blog.csdn.net/tju_tahara

hdu 2363 Cycling (最短路spfa + 暴力枚举)

Cycling Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

POJ:2914 Minimum Cut(暴力枚举并查集)

Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others...
  • zugofn
  • zugofn
  • 2016年08月01日 20:13
  • 349

hdoj 1598 find the most comfortable road 【并查集】+【暴力枚举】

find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/3276...

HDU 2363 Cycling(枚举+最短路)

http://acm.hdu.edu.cn/showproblem.php?pid=2363 题目大意: n个点,没个点都有不同的高度,同时n个点之间有m条路,题目要求从第1个点到第n个点,在...

HDU 2363 Cycling 最短路+枚举

Cycling Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

HDU 2363 Cycling 枚举限制+最短路

题意:给n个点即n个点的高度,问一条最短路的最大高度差最小值是多少? 想法:枚举所有两个点之间的高度差,然后把这个限制用最短路去跑,可以完成的最小的限制即是答案,还有,不要写无用的函数在上面...

HDU 2363 Cycling(二分+枚举+限制最短路,好题)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=2363 题目大意: 小明从家里走到学校去考试, 路上有各个交叉点,它们有自己的海拔高度。...

hdu 5556 Land of Farms【思维+暴力枚举+最大独立集】好题!

Land of Farms Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)...

hdu 5128 The E-pang Palace(计算几何,暴力枚举)

The E-pang PalaceProblem Description E-pang Palace was built in Qin dynasty by Emperor Qin Shihuang...

hdu 5908 Abelian Period【思维+暴力枚举】

Abelian Period Accepts: 288 Submissions: 984 Time Limit: 2000/1000 MS (Java/Others) Memo...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU 2363 Cycling(并查集 SPFA 暴力枚举)
举报原因:
原因补充:

(最多只允许输入30个字)