hdu2962 shortest path

The main idea of ​​the title: given the starting point and ending point and the weight of the truck, ask you what is the shortest path under the premise of ensuring the maximum weight of the truck, we can enumerate the weight of the truck, and perform dijkstra on the weight of each truck, Calculate whether there is a shortest path to the end point. Of course, enumeration can also be optimized with two points, and the time difference is quite a lot.

             
  2016-08-05 15:23:00 Accepted 2962 9297MS 9500K 2257 B G++  

 

   2016-08-05 15:34:54 Accepted 2962 1606MS 9496K 2516 B G++  

The one below is a binary optimized one.

//two points + dijkstra
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f3f
int n,m,ch,h,l,c,r,s,e,ans1,ans2;
bool visited[1005];
int a[1005][1005],height[1005][1005];
int dist[1005];
int hei [1005];
void dijkstra(int he)
{
    memset(visited,false,sizeof(visited));
    memset(dist,0x3f,sizeof(dist));
    visited[s]=true;
    for(int i=1;i<=n;i++)
    {
        hei[i]=height[s][i];
        if(hei[i]<he)
            dist[i]=INF;
        else
            dist[i]=a[s][i];

    }
    for(int i=1;i<=n;i++)
    {
        int MIN=INF,node=-1;
        for(int j=1;j<=n;j++)
        {
            if(!visited[j]&&dist[j]<MIN&&hei[j]>=he)
            {
                MIN=dist[j];
                node=j;
            }
        }

        if(node==-1)
            return;
        visited[node]=true;

        for(int j=1;j<=n;j++)
        {
            if(!visited[j]&&height[node][j]>=he&&dist[j]>dist[node]+a[node][j])
            {
                dist[j]=dist[node]+a[node][j];
                hei[j]=height[node][j];
            }
        }

    }

}
intmain()
{
    int sym=0;
    while(true)
    {
        sym ++;
        ans1=-1;
        ans2=-1;
        memset(a,0x3f,sizeof(a));
        memset(height,0,sizeof(height));
        scanf("%d%d",&n,&m);
        if(n==0&&m==0)
            break;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d%d",&l,&r,&h,&c);
            a[l][r]=a[r][l]=c;
            if(h==-1)
                height[l][r]=height[r][l]=INF;
            else
                 height[l][r]=height[r][l]=h;
        }
        for(int i=1;i<=n;i++)
        {
            a[i][i]=height[i][i]=0;
        }
        scanf("%d%d%d",&s,&e,&ch);
        int left=1;
        int right=ch;
       int mid=0;
        while(left<=right)
        {
            mid=(left+right)/2;
            dijkstra(mid);
            if(dist[e]!=INF)
            {
                ans1=dista[e];
                ans2=mid;
                left=mid+1;
            }
            else
                right=mid-1;
        }
        if(sym!=1)
            printf("\n");
        printf("Case %d:\n",sym);
        if(ans1==-1&&ans2==-1)
        {
            printf("cannot reach destination\n");
        }
        else
        {
            printf("maximum height = %d\n",ans2);
            printf("length of shortest route = %d\n",ans1);
        }

    }
    return 0;
}




// enumeration + dijkstra
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f3f
int n,m,ch,h,l,c,r,s,e;
bool visited[1005];
int a[1005][1005],height[1005][1005];
int dist[1005];
int hei [1005];
void dijkstra(int he)
{
    memset(visited,false,sizeof(visited));
    memset(dist,0x3f,sizeof(dist));
    visited[s]=true;
    for(int i=1;i<=n;i++)
    {
        hei[i]=height[s][i];
        if(hei[i]<he)
            dist[i]=INF;
        else
            dist[i]=a[s][i];

    }
    for(int i=1;i<=n;i++)
    {
        int MIN=INF,node=-1;
        for(int j=1;j<=n;j++)
        {
            if(!visited[j]&&dist[j]<MIN&&hei[j]>=he)
            {
                MIN=dist[j];
                node=j;
            }
        }

        if(node==-1)
            return;
        visited[node]=true;

        for(int j=1;j<=n;j++)
        {
            if(!visited[j]&&height[node][j]>=he&&dist[j]>dist[node]+a[node][j])
            {
                dist[j]=dist[node]+a[node][j];
                hei[j]=height[node][j];
            }
        }

    }

}
intmain()
{
    int sym=0;
    while(true)
    {
        sym ++;
        memset(a,0x3f,sizeof(a));
        memset(height,0,sizeof(height));
        scanf("%d%d",&n,&m);
        if(n==0&&m==0)
            break;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d%d",&l,&r,&h,&c);
            a[l][r]=a[r][l]=c;
            if(h==-1)
                height[l][r]=height[r][l]=INF;
            else
                 height[l][r]=height[r][l]=h;
        }
        for(int i=1;i<=n;i++)
        {
            a[i][i]=height[i][i]=0;
        }
        scanf("%d%d%d",&s,&e,&ch);
        while(ch!=0)
        {

            dijkstra(ch);
            if(dist[e]!=INF)
            {
                break;
            }
            ch--;
        }
        if(sym!=1)
            printf("\n");
        printf("Case %d:\n",sym);
        if(ch==0)
        {
            printf("cannot reach destination\n");
        }
        else
        {
            printf("maximum height = %d\n",ch);
            printf("length of shortest route = %d\n",dist[e]);
        }

    }
    return 0;
}



 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326885994&siteId=291194637