您的位置:首页 > 其它

zoj 1857 Fire Station(重写 SPFA+邻接表~)

2010-11-30 22:43 351 查看
邻接表+DIJ+优先队列 过了。



这次吧SPFA学会了,就又写了一个。



深刻理解了SPFA。。。



那个hash函数是记录是否在队列里。。。无语。。



#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <limits.h>
#include <queue>
#define N 600
using namespace std; 
struct adjlist
{
    int length;
    int num;
    struct adjlist *next;
};
struct dist
{
    int num;
    int length;
};
typedef  struct adjlist adj;
typedef  struct dist d;
queue<int> Q;
adj *p
,node[N*N],*head
;
d tempdis
,temp;
int point
;
/*******************************************main********************************************/
int main(void)
{
    int fnum,n,i,j,max,k,mmax;
    int hash
,fire,x,y,length,flag
;
    int count,tempj,mark,dis
,t
;
    char str[50];
    while( scanf("%d %d",&fnum,&n)!= EOF )
    {
        memset( p,'/0',sizeof(p) );
        memset(head,'/0',sizeof(head));
        memset( flag,0,sizeof(flag) );
        memset( dis,0,sizeof(dis));
        mark = 0;
        for(i=1; i<=fnum; i++) //输入已经有消防站的岔口 
        {
            scanf("%d",&fire);
            flag[fire] = 1;
            dis[fire] = 0;
        }
        getchar();
        count = 1;
        while( gets(str) && strlen(str) ) 
        {
            sscanf(str,"%d%d%d",&x,&y,&length);
            node[count].num = y;
            node[count].length = length;
            node[count].next = p[x];
            p[x] = &node[count];
            count++;
            node[count].num = x;
            node[count].length = length;
            node[count].next = p[y];
            p[y] = &node[count];
            count++;
        }
        for(j=1; j<=n; j++)
        {
            if(flag[j] == 0)
                mark = 1;
        }
        if( mark == 0 )
        {
            printf("1/n");
            continue;
        }
        /**********************************************************************************/         
        max = INT_MAX;
        for(i=1; i<=n; i++)
        	if( !flag[i] )
        		dis[i] = INT_MAX;
        memcpy(t,dis,(n+1)*sizeof(int)); // Save dis.
        /*******************************************************************************/
        for(j=1; j<=n; j++)
        {            
            if( flag[j] == 1 )
                continue;     
            memcpy(dis,t,(n+1)*sizeof(int));
            memset( hash,0,sizeof(hash) );
            mmax = 0;             
			for(i=1; i<=n; i++)
                head[i] = p[i];
            dis[j] = 0;  hash[j] = 1;
        	for(i=1; i<=n; i++)
            	if( flag[i] )
            	{
					Q.push(i);
					dis[i] = 0;
				}
			Q.push(j);
            while( !Q.empty() )
            {
				int x = Q.front();
				Q.pop(); hash[x] = 0;
				while( head[x]!=NULL )
				{
					int y = head[x]->num,len = head[x]->length;
					if( dis[y] > dis[x] + len )
					{
						dis[y] = dis[x] + len;
						if( !hash[y] )
						{
							Q.push(y);
							hash[y] = 1;
						}
					}
					head[x] = head[x]->next;
				}
			} 
        /**************************************************************************/ 	               
            for(k=1; k<=n; k++)
                if( dis[k] > mmax )
                    mmax = dis[k];
            if( max > mmax )
            {
                max = mmax;
                tempj = j;
            } 
        }
        printf("%d/n",tempj); 
    }  
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: