UVALive - 4223 dijksta

这题的题意就是给你城市的数量n,道路的数量m,然后m组数据,每组数据有城市A到城市B,道路的限制高度,道路的长度。然后最后一行就是给你城市X到城市Y,卡车最高的高度。题目要你求出卡车从城市X到城市Y运送的最多货物,并且求出最多货物下的最短路径。

这道题目我是先用djkstra算法求出卡车从城市X到城市Y的最大货物高度,然后以这个为限制,再用一遍dijksta算法,求出限制高度在这个之上的最短路径。

格式要求有点严格,最后一行结束的时候不能输出两个空行。

# include <stdio.h>
# include <string.h>
# include <algorithm>
# include <math.h>
#define maxn 0x3f3f3f3f
#include<iostream>
using namespace std;
int m,n,vis1[1100],vis2[1100],x,y,z;
int hei;
int a[1100][1100],b[1100][1100];
int dijkstra_height()
{
    int h[1100],mann,flag;
    memset(h,0,sizeof(h));
    vis1[x]=1;
    int minn,i,j;
    for(i=1;i<=m;i++){
        if(b[x][i]==-1){
            h[i]=z;
        }
        else{
            h[i]=b[x][i];
        }
    }
    for(i=0;i<m;i++){
        mann=-1;
        for(j=1;j<=m;j++){
            if(!vis1[j]&&h[j]>mann){
                mann=h[j];
                flag=j;
            }
        }
        if(mann==-1){
            break;
        }
    //    cout<<flag<<"36"<<endl;
        vis1[flag]=1;
        for(j=1;j<=m;j++)
        {
            if(b[flag][j]==-1){
                b[flag][j]=z;
            }
            if(!vis1[j]&&h[j]<=h[flag]&&h[j]<=b[flag][j]){
                h[j]=min(h[flag],b[flag][j]);
            }
        }
    //    cout<<h[flag]<<" "<<b[flag][4]<<endl;
    //    cout<<h[4]<<"*"<<endl;
    }
    return h[y];
}
int dijkstra_long()
{
    int l[1100];
    memset(l,maxn,sizeof(l));
    int flag,i,j,minn;
    vis2[x]=1;
    for(i=1;i<=m;i++){
        if(!vis2[i]&&(b[x][i]>=hei||b[x][i]==-1)){
            l[i]=a[x][i];
    //        cout<<l[i]<<i<<endl;
        }
    }
//    cout<<vis2[2]<<" "<<b[1][2]<<" "<<l[2];
//    cout<<l[2]<<endl;
    for(i=0;i<m;i++){ 
        minn=maxn;
        for(j=1;j<=m;j++){
            if(!vis2[j]&&minn>l[j]){
                minn=l[j];
                flag=j;
            }
        }
    //    cout<<minn<<"71"<<endl;
    //    cout<<flag<<endl;
        if(minn==maxn){
            break;
        }
        vis2[flag]=1;
        for(j=1;j<=m;j++){
            if(!vis2[j]&&l[j]>(l[flag]+a[flag][j])&&b[flag][j]>=hei){
    //            cout<<l[j]<<" "<<j<<" "<<l[flag]<<a[flag][j]<<endl;
                l[j]=l[flag]+a[flag][j];
            }
        }
    }
    return l[y];
}
int main()
{
    int ans,e,f,c,l,cas;
    cas=1;
    while(cin>>m>>n&&(m+n)){
        if(cas!=1){
            cout<<endl;
        } 
        memset(vis1,0,sizeof(vis1));
        memset(vis2,0,sizeof(vis2));
        memset(a,maxn,sizeof(a));
        memset(b,0,sizeof(b));
        while(n--){
            cin>>e>>f>>c>>l;
            b[e][f]=c;
            b[f][e]=c;
            a[e][f]=l;
            a[f][e]=l;
        }
        cin>>x>>y>>z;
        hei=dijkstra_height();
        //cout<<hei<<"106"<<endl;
        if(hei>z){
            hei=z;
        }
        ans=dijkstra_long();
        if(ans==maxn){
            cout<<"Case "<<cas++<<":"<<endl;
            cout<<"cannot reach destination"<<endl;
        }
        else{
            cout<<"Case "<<cas++<<":"<<endl;
            cout<<"maximum height = "<<hei<<endl;
            cout<<"length of shortest route = "<<ans<<endl;
        }
    }
    return 0;
}

 

posted @ 2018-08-11 09:26  .。  阅读(98)  评论(0编辑  收藏  举报