hdu 2962 最短路+二分

题意:最短路上有一条高度限制,给起点和最大高度,求满足高度最大情况下,最短路的距离

不明白为什么枚举所有高度就不对

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 const int maxint=999999;
  5 int c[1005][1005][2],dist[1005],H[1005];
  6 using namespace std;
  7 int n,line;
  8 int i,j,k;
  9 int st,ed,mid;
 10 int t=0;
 11 int path[1005];
 12 int visit[1005];
 13 bool dijkstra()
 14 {
 15     bool s[1005];
 16      for(int i=1;i<=n;i++)
 17      {
 18              s[i]=0;
 19              if(c[st][i][0]>=mid)
 20                   dist[i]=c[st][i][1];
 21               else dist[i]=maxint;
 22             path[i]=st;
 23      }
 24      s[st]=1;
 25      for(int i=1;i<n;i++)
 26      {
 27          int temp=maxint;
 28          int u=st;
 29            for(int j=1;j<=n;j++)
 30                  if(!s[j]&&temp>dist[j]&&c[path[j]][j][0]>=mid)
 31                  {
 32                         temp=dist[j];
 33                         u=j;
 34                  }
 35            if(temp==maxint) break;
 36            s[u]=1;
 37            if(u==ed) return true;
 38            for(int j=1;j<=n;j++)
 39                  if(!s[j]&&c[u][j][1]!=maxint&&c[u][j][0]>=mid)
 40                       if(dist[j]>dist[u]+c[u][j][1])
 41                       {
 42                                dist[j]=dist[u]+c[u][j][1];
 43                                path[j]=u;
 44                       }
 45      }
 46    return false;
 47 }
 48 int main()
 49 {
 50        #ifndef ONLINE_JUDGE
 51         freopen("1.in","r",stdin);
 52         #endif
 53     while(scanf("%d%d",&n,&line)!=EOF&&n&&line)
 54     {
 55         int tot=1;
 56         memset(H,0,sizeof(H));
 57         H[0]=0;
 58         if(t)   printf("
");
 59         printf("Case %d:
",++t);
 60         for(i=1;i<=n;i++)
 61             for(j=1;j<=n;j++)
 62         {
 63             c[i][j][1]=(i==j?0:maxint);
 64             c[i][j][0]=-999999;
 65         }
 66         for(i=1;i<=line;i++)
 67         {
 68             int p,q,len,h;
 69             scanf("%d%d%d%d",&q,&p,&h,&len);
 70             if(h==-1)
 71             {
 72                 c[p][q][0]=c[q][p][0]=maxint;
 73                 c[p][q][1]=c[q][p][1]=len;
 74                 continue;
 75             }
 76             if(c[p][q][0]<h)
 77             {
 78                 c[p][q][0]=c[q][p][0]=h;
 79                 c[p][q][1]=c[q][p][1]=len;
 80             }
 81         }
 82         int tall;
 83         scanf("%d%d%d",&st,&ed,&tall);
 84         /*if(st==ed)
 85         {
 86             printf("maximum height = %d
",tall);
 87            printf("length of shortest route = 0
");
 88            continue;
 89         }*/
 90         /*H[tot++]=tall;
 91         sort(H,H+tot);
 92         int ans=0;
 93         bool flag=0;
 94         for(i=tot-1;i>=0;i--)
 95         {
 96             if(H[i]>tall)   continue;
 97             mid=H[i];
 98             if(dijkstra())
 99             {
100                 flag=1;
101                 ans=dist[ed];
102                 break;
103             }
104         }*/
105         int first=1;
106         int ans;
107         mid=(first+tall)>>1;
108         while(first<=tall)
109         {
110             if(dijkstra())
111             {
112                 ans=dist[ed];
113                 first=mid+1;
114             }
115             else
116                 tall=mid-1;
117             mid=(first+tall)>>1;
118         }
119         if(tall==0) printf("cannot reach destination
");
120         else
121         {
122            printf("maximum height = %d
",tall);
123            printf("length of shortest route = %d
",ans);
124         }
125     }
126     return 0;
127 }
原文地址:https://www.cnblogs.com/cnblogs321114287/p/4454121.html