E - Cycling HDU - 2363

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!) 
InputOn 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 <= h i <= 1 000 000 000): the altitude of the i-th crossing. 

m lines with three integers a j , b j (1 <= a j , b j <= n) and c j (1 <= c j <= 1 000 000): this indicates that there is a two-way road between crossings a j and b jof length c j . 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. 
OutputFor 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

題目要求:求出從1到n高度差最小的路徑,並且路徑的長度最短。

題解:參考了網上的題解。先將所有點按高度從小到大排序,利用暴力枚舉求出使1與n連通的高度差最小的一段或者幾段,再跑一遍spfa,求出最短的路徑。

(排序後邊對應的下標會改變,一定要處理好細節問題)

#include<iostream>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int inf=0x3f3f3f3f;
struct height{
    int index,hh;
    friend bool operator<(height a,height b){
        return a.hh<b.hh;
    }
}h[105];
struct node{
    int v,w;
    node(){}
    node(int vv,int ww){
        v=vv,w=ww;
    }
};
struct stu{
    int s,e;
}tmp;
int fa[105],dis[105],n,m,minh,minw;
bool vis[105],v[105];
vector<node> e[105];
queue<stu> q;
queue<int> qq;
int getfa(int num){
    return fa[num]==num?num:getfa(fa[num]);
}
void comb(int a,int b){
    int afa=getfa(a),bfa=getfa(b);
    if(afa!=bfa) fa[afa]=bfa;
}
int spfa(){
    for(int i=2;i<=n;i++) dis[i]=inf;
    dis[1]=0,vis[1]=1,qq.push(1);
    while(!qq.empty()){
        int now=qq.front();vis[now]=0,qq.pop();
        for(int i=0;i<e[now].size();i++)
        if(v[e[now][i].v]&&dis[e[now][i].v]>dis[now]+e[now][i].w){
            dis[e[now][i].v]=dis[now]+e[now][i].w;
            if(!vis[e[now][i].v]) qq.push(e[now][i].v),vis[e[now][i].v]=1;
        }
    }
    return dis[n];
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        minh=minw=inf;
        for(int i=1;i<=n;i++) scanf("%d",&h[i].hh),h[i].index=i,e[i].clear();
        sort(h+1,h+n+1);
        for(int u,v,w,i=0;i<m;i++){
            scanf("%d%d%d",&u,&v,&w);
            e[u].push_back(node(v,w));
            e[v].push_back(node(u,w));
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++) fa[j]=j,v[j]=0;
            int j=i;
            while(j<=n&&getfa(1)!=getfa(n)){
                v[h[j].index]=1;
                for(int k=0;k<e[h[j].index].size();k++)
                    if(v[e[h[j].index][k].v]) comb(h[j].index,e[h[j].index][k].v);
                j++;
            }
            if(getfa(1)==getfa(n)){
                j--;
                if(h[j].hh-h[i].hh<minh){
                    //printf("%d %d %d\n",h[i].index,h[j].index,minh);
                    minh=h[j].hh-h[i].hh;
                    tmp.s=i,tmp.e=j;
                    while(!q.empty()) q.pop();
                    q.push(tmp);
                }
                else if(h[j].hh-h[i].hh==minh){
                    tmp.s=i,tmp.e=j;
                    q.push(tmp);
                }
            }
        }
        while(!q.empty()){
            tmp=q.front(),q.pop();
            for(int i=1;i<=n;i++) v[i]=0;
            for(int i=tmp.s;i<=tmp.e;i++) v[h[i].index]=1;
            minw=min(minw,spfa());
        }
        if(n==1)
            printf("0 0\n");
        else
            printf("%d %d\n",minh,minw);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章