zoj 1857 Fire Station(重写 SPFA+邻接表~)
2010-11-30 22:43
351 查看
邻接表+DIJ+优先队列 过了。
这次吧SPFA学会了,就又写了一个。
深刻理解了SPFA。。。
那个hash函数是记录是否在队列里。。。无语。。
这次吧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; }
相关文章推荐
- ZOJ 1857 && POJ 2607 Fire Station【SPFA】
- zoj 1082 Stockbroker Grapevine( 重写 SPFA!)
- ZOJ 2008 Invitation Cards(邻接表+spfa||dijkstra)
- ZOJ-1857(POJ-2607) Fire Station
- POJ 2607 / ZOJ 1857 Fire Station (临接表+spfa)
- zoj 1857 Fire Station还是最短路。。
- zoj 1857 || poj 2607 Fire Station(纠结!!!)
- zoj 3088 Easter Holidays (spfa)
- SPFA 邻接表模式
- zoj 1935 || poj 1932 XYZZY(SPFA+Floyd)
- CSU - 1333~Funny Car Racing(spfa+邻接表)
- 【算法系列学习】SPFA邻接表最短路 [kuangbin带你飞]专题四 最短路练习 F - Wormholes
- poj 2607 Fire Station (spfa)
- zoj 3946 Highway Project spfa
- JOJ 1089 & ZOJ 1060 & poj 1094 Sorting It All Out (邻接表的栈拓扑排序模板)
- HDOJ 1874 畅通工程续(最短路径,dijkstra算法,spfa邻接表,floyd算法)
- zoj 2008 单源最短路 SPFA
- POJ-3259-Wormholes(SPFA 链表建立邻接表)
- poj1511——Invitation Cards(SPFA+邻接表)
- 【图论】【单源最短路】【SPFA】pascal+邻接表