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

求测试数据,自环,关系矛盾都考虑

Posted by foreverlin at 2009-09-28 00:48:36 on Problem 2883
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#define MAX 2000000 
using namespace std;
typedef struct node
{
    int s,e,d;    
    }node;
node v[80001];//x轴上的关系
node w[80001];//y轴上的关系
int lenv,lenw;    
int dis[501];
int n,m,check; 
int dx[501][501]; 
void addedge(int x,int y,char *c)
{
    int i;
    if(strcmp(c,"N")==0)//N
    {   
       if(dx[x][y]!=0&&dx[x][y]!=1)check=1;
       dx[x][y]=1;       
       v[lenv].s=x;
       v[lenv].e=y;
       v[lenv++].d=1;
       v[lenv].s=y;
       v[lenv].e=x;
       v[lenv++].d=-1;          
       }             
    if(strcmp(c,"E")==0)//S 
    { 
       if(dx[x][y]!=0&&dx[x][y]!=2)check=1;
       dx[x][y]=2;                      
       v[lenv].s=x;
       v[lenv].e=y;
       v[lenv++].d=-1;
       v[lenv].s=y;
       v[lenv].e=x;
       v[lenv++].d=1;                     
       }
    if(strcmp(c,"S")==0)//E
    { 
       if(dx[x][y]!=0&&dx[x][y]!=3)check=1;
       dx[x][y]=3;                    
       w[lenw].s=x;
       w[lenw].e=y;
       w[lenw++].d=1;
       w[lenw].s=y;
       w[lenw].e=x;
       w[lenw++].d=-1;                      
       }
    if(strcmp(c,"W")==0)//W
    {
       if(dx[x][y]!=0&&dx[x][y]!=4)check=1;
       dx[x][y]=4;           
       w[lenw].s=x;
       w[lenw].e=y;
       w[lenw++].d=-1;
       w[lenw].s=y;
       w[lenw].e=x;
       w[lenw++].d=1;                    
       }
    if(strcmp(c,"NE")==0)//NE
    {
       if(dx[x][y]!=0&&dx[x][y]!=5)check=1;
       dx[x][y]=5;
       v[lenv].s=x;
       v[lenv].e=y;
       v[lenv++].d=1;
       v[lenv].s=y;
       v[lenv].e=x;
       v[lenv++].d=-1; 
       
       w[lenw].s=x;
       w[lenw].e=y;
       w[lenw++].d=1;
       w[lenw].s=y;
       w[lenw].e=x;
       w[lenw++].d=-1;                    
       }
    if(strcmp(c,"NW")==0)//NW
    {
       if(dx[x][y]!=0&&dx[x][y]!=6)check=1;
       dx[x][y]=6;
       v[lenv].s=x;
       v[lenv].e=y;
       v[lenv++].d=1;
       v[lenv].s=y;
       v[lenv].e=x;
       v[lenv++].d=-1; 
       
       w[lenw].s=x;
       w[lenw].e=y;
       w[lenw++].d=-1;
       w[lenw].s=y;
       w[lenw].e=x;
       w[lenw++].d=1;                    
       }      
    if(strcmp(c,"SE")==0)//SE
    {
       if(dx[x][y]!=0&&dx[x][y]!=7)check=1;
       dx[x][y]=7;
       v[lenv].s=x;
       v[lenv].e=y;
       v[lenv++].d=-1;
       v[lenv].s=y;
       v[lenv].e=x;
       v[lenv++].d=1;
       
       w[lenw].s=x;
       w[lenw].e=y;
       w[lenw++].d=1;
       w[lenw].s=y;
       w[lenw].e=x;
       w[lenw++].d=-1;                      
       }
    if(strcmp(c,"SW")==0)//SW
    {
       if(dx[x][y]!=0&&dx[x][y]!=8)check=1;
       dx[x][y]=8;
       v[lenv].s=x;
       v[lenv].e=y;
       v[lenv++].d=-1;
       v[lenv].s=y;
       v[lenv].e=x;
       v[lenv++].d=1;  
       
       w[lenw].s=x;
       w[lenw].e=y;
       w[lenw++].d=-1;
       w[lenw].s=y;
       w[lenw].e=x;
       w[lenw++].d=1;                       
       }                  
    }
        
bool bellman_ford(int k)
{
    int i,j,flag;
    for(i=1;i<=n;i++)dis[i]=2000000;
    dis[1]=0;
//    memset(dis,0,sizeof(dis));
    if(k==0)
    {
       for(j=0;j<n;j++)
       {
           flag=1;            
           for(i=0;i<lenv;i++)
           {
               if(dis[v[i].e]>dis[v[i].s]+v[i].d)
               {
                  flag=0;
                  dis[v[i].e]=dis[v[i].s]+v[i].d;                                 
                  }              
               }            
           if(flag)return 1;    
           } 
       return 0;    
       }
    if(k==1)
    {
       for(j=0;j<n;j++)
       {
           flag=1;            
           for(i=0;i<lenw;i++)
           {
               if(dis[w[i].e]>dis[w[i].s]+w[i].d)
               {
                  flag=0;
                  dis[w[i].e]=dis[w[i].s]+w[i].d;                                 
                  }              
               }            
           if(flag)return 1;    
           } 
       return 0;             
       }   
    }        
    
int main()
{
    int i,j,k,t,x,y;
    scanf("%d",&t);
    char b[100];
    while(t--)
    {
          scanf("%d%d",&n,&m);    
          lenv=lenw=0;
          memset(dx,0,sizeof(dx));
          check=0;
          for(i=1;i<=m;i++)
          {
              scanf("%d%s%d",&x,b,&y);
              if(x==y)check=1;
              addedge(x,y,b);       
              }
          if(check){printf("IMPOSSIBLE\n");continue;}                                  
          if(bellman_ford(0)==0||bellman_ford(1)==0)printf("IMPOSSIBLE\n");
          else printf("POSSIBLE\n");              
          }
    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