Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

floyd求最短路加一下限制过的

Posted by Hyoga at 2012-07-04 15:39:24 on Problem 1062
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int n,m,p,l0,x,lmin[300][300],lmax[300][300],f[300][300],l[300],t,v;
int main()
{
 scanf("%d%d",&m,&n);
 memset(f,-1,sizeof(f));
 for(int i=1;i<=n;i++)
 {
  scanf("%d%d%d",&p,&l0,&x);
  f[n+i][i]=p;l[i]=l0;l[n+i]=l0;
  for(int j=1;j<=x;j++)
  {
   scanf("%d%d",&t,&v);
   f[t][i]=v;
  }
 }
 n=2*n;
 for(int i=1;i<=n;i++)
  for(int j=1;j<=n;j++)
  if(f[i][j]!=-1)
  {
   lmin[i][j]=l[i]<l[j]?l[i]:l[j];
   lmax[i][j]=l[i]>l[j]?l[i]:l[j];
  }

  for(int k=1;k<=n;k++)
   for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    if(f[i][k]!=-1 && f[k][j]!=-1 &&
       abs(lmin[i][k]-lmin[k][j])<=m && abs(lmax[i][k]-lmax[k][j])<=m
      && abs(lmin[i][k]-lmax[k][j])<=m && abs(lmax[i][k]-lmin[k][j])<=m )
    if(f[i][k]+f[k][j]<f[i][j] || f[i][j]==-1)
    {
     f[i][j]=f[i][k]+f[k][j];
     lmin[i][j]=lmin[i][k]<lmin[k][j]?lmin[i][k]:lmin[k][j];
     lmax[i][j]=lmax[i][k]>lmax[k][j]?lmax[i][k]:lmax[k][j];
    }

 int minn=1<<30;
 for(int i=n/2+1;i<=n;i++)
 if(f[i][1]!=-1 && f[i][1]<minn)minn=f[i][1];

 printf("%d\n",minn);
 return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator