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

求指点,在vc6.0上可以,但是WA

Posted by 652107 at 2011-04-21 21:24:54 on Problem 1158
//Dijkstra算法
#include<iostream>
#define N 301
using namespace std;
int Road[N][N];     //路的信息,不用0下标,不可达用infinite;
enum color{B,P};    //颜色
const int infinite=99999;
struct junction
   {
     color State;   //初始颜色
      int InitT;  //初始变化时间
      int BT;   //蓝色持续时间
      int PT;   //紫色持续时间
   }junct[N];      //路口信息,不使用下标0
struct{
    int p;  //记录前一个点
    int t; //到该点的最短距离
  }path[N];     //点的路径
int lowT[N];  //各点到已加入点的最短时间 

int FindMinRoad(int s,int e,int n)    //寻找最短时间 ,s为起始点,e终点,n为实际路口点的个数
   { 
     void update(int current,int n);  //根据当前加入的点更新lowT和path数组
     int find_minT(int n);    //从lowT中找到时间最小但不为0的点,返回点号

     int waitT(int current,int next);  //根据current点,下一个点的最小等待时间,并返回
     for(int i=1;i<=n;i++)  //将点s加入,初始化
       {
         if(i==s){lowT[i]=0;continue;}
         path[i].p=s;  
         path[i].t=lowT[i]=waitT(s,i)+Road[s][i];     
       }
					int current=find_minT(n);
     while(current!=e)//未到达终点
       {
         update(current,n);  //更新lowT和path数组
         current=find_minT(n);//找到一个最小的时间号
									if(current==0)return 0;  //无法到达返回0;
       }
     return path[e].t;  //返回到达点e的最短时间    
   }
//在lowT中找不为0但最小的点号
int find_minT(int n)
  {
     int min=infinite;
     int t=0;          //无最小的点
     for(int i=1;i<=n;i++)
     if(lowT[i]!=0&&lowT[i]<min){min=lowT[i];t=i;}
     return t; 
  }
void update(int current,int n)
  {
    int waitT(int current,int next);  //根据current点,下一个点的最小等待时间,并返回
    for(int i=1;i<=n;i++)
      {
        if(lowT[i]==0)continue;
        if(i==current){lowT[i]=0;continue;}
        int t=waitT(current,i)+Road[current][i];
        if(t+path[current].t<lowT[i]){path[i].p=current;
								  path[i].t=t+path[current].t;lowT[i]=t;}
      }
  }
int waitT(int current,int next)  //求等待时间
  {
    color get_color(int m,int T,int *t);  //求点m的当前状态,t为时间余量,T为已经经历的时间
    if(junct[current].State==junct[next].State&&junct[current].InitT==junct[next].InitT
      &&junct[current].BT==junct[next].PT&&junct[current].PT==junct[next].BT)return infinite;  //不可通过
				if(Road[current][next]==infinite)return infinite;
    int t=path[current].t;  //已经经过的时间
    color s1,s2;  //两个点当前的状态
    int t1,t2;
    s1=get_color(current,t,&t1);
    s2=get_color(next,t,&t2);     //求点的当前状态
    if(s1==s2)return 0;    //颜色相同等待时间为0
    if(t1<t2)return t1;
    if(t1>t2)return t2;
    if(s1==B)
     {
       if(junct[current].PT<junct[next].BT)return t1+junct[current].PT;
       if(junct[current].PT>junct[next].BT)return t1+junct[next].BT;
       if(junct[current].BT<junct[next].PT)return t1+junct[current].PT+junct[current].BT;
       else return t1+junct[current].PT+junct[next].PT;
     }
    if(junct[current].BT<junct[next].PT)return t1+junct[current].BT;
    if(junct[current].BT>junct[next].PT)return t1+junct[next].PT;
    if(junct[current].PT<junct[next].BT)return t1+junct[current].BT+junct[current].PT;
    else return t1+junct[current].BT+junct[next].BT;
  }
color get_color(int m,int T,int *t)
  { 
    if(T<junct[m].InitT){*t=junct[m].InitT-T;return junct[m].State;}
    int mt=junct[m].InitT;
    color s;
    if(junct[m].State==B)s=P;
    else s=B;
    while(1)
      {
        if(s==B)
         {
           mt+=junct[m].BT;
           if(mt>T){*t=mt-T;return B;}
           mt+=junct[m].PT;
           if(mt>T){*t=mt-T;return P;}
         }
        mt+=junct[m].PT;
        if(mt>T){*t=mt-T;return P;}
        if(mt>T){*t=mt-T;return B;}
      }
  }
int main()
  {
    int s,e,n,m;
    char h;
    cin>>s>>e;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
     {
       cin>>h>>junct[i].InitT>>junct[i].BT>>junct[i].PT;
       if(h=='B')junct[i].State=B;
        else junct[i].State=P;
     }
    for(i=1;i<=n;i++)
     for(int j=1;j<=n;j++){if(i==j)Road[i][j]=0;else Road[i][j]=infinite;}
    int han,lie,t;
    for(int j=0;j<m;j++)
      {
        cin>>han>>lie>>t;
        Road[han][lie]=t;
        Road[lie][han]=t;
      }
    cout<<FindMinRoad(s,e,n)<<endl;
    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