spfa经过deque(双向队列)优化后的模板
//
C
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include < string.h>
#include <ctype.h>
#include <time.h>
// C++
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <list>
using namespace std;
// start
// C
typedef long long LL;
// typedef __int64 LL;
#define INF 0x7F7F7F7F
#define HEAD 100
#define EDGE 10000
#define eps 1e-8
typedef pair < int , int> PII;
#define MP make_pair
#define CLEAR(a,b) memset ( a , b , sizeof ( a ) )
#define CLEARN(a,b,s,size) memset ( a + s , b , sizeof size )
#define FOR(a,b,i) for ( i = a ; i < b ; i++ )
#define FORB(b,a,i) for ( i = b ; i > a ; i-- )
#define FORE(a,b,i) for ( i = a ; i <= b ; i++ )
#define FOREB(b,a,i) for ( i = b ; i >= a ; i-- )
#define FORN(a,b,i,nc) for ( i = a ; i < b ; i += nc )
#define FORNB(b,a,i,nc) for ( i = b ; i > a ; i -= nc )
#define FOREN(a,b,i,nc) for ( i = a ; i <= b ; i += nc )
#define FORENB(b,a,i,nc) for ( i = b ; i >= a ; i -= nc )
#define COPY(a,b,lt,rt,i) for ( i = lt ; i <= rt ; i++ ) a[i] = b[i];
#define sqr(a,b) sqrt ( (double) a * a + (double) b * b )
// c++
template < class T> inline T INT_LEN( T v ) { int len = 1 ; while ( v /= 10 ) ++len; return len; }
// #define LocalHost
int M = EDGE;
bool vis[HEAD];
int dis[HEAD] , q[EDGE];
int N;
void spfa ( int * head , re * edge , int s , int M ){ // re 为a边À?的Ì?类¤¨¤型¨ª
int i , u , v , front = 0 , rear = 1 ;
CLEAR ( vis , 0 );
CLEAR ( dis , 0x7f );
dis[s] = 0;
q[ 0] = s;
while ( front != rear ){
u = q[front++] ;
if ( front == M ) front = 0;
vis[u] = false;
for ( i = head[u] ; ~i ; i = edge[i].nxt ){
v = edge[i].v;
if ( dis[v] > dis[u] + edge[i].w ){
dis[v] = dis[u] + edge[i].w ;
if ( !vis[v] ){
vis[v] = true;
if ( dis[v] > dis[q[front]] ){
q[rear++] = v;
if ( rear == M ) rear = 0;
}
else {
if ( --front < 0 ) front = ~-M;
q[front] = v;
}
}
}
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include < string.h>
#include <ctype.h>
#include <time.h>
// C++
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <list>
using namespace std;
// start
// C
typedef long long LL;
// typedef __int64 LL;
#define INF 0x7F7F7F7F
#define HEAD 100
#define EDGE 10000
#define eps 1e-8
typedef pair < int , int> PII;
#define MP make_pair
#define CLEAR(a,b) memset ( a , b , sizeof ( a ) )
#define CLEARN(a,b,s,size) memset ( a + s , b , sizeof size )
#define FOR(a,b,i) for ( i = a ; i < b ; i++ )
#define FORB(b,a,i) for ( i = b ; i > a ; i-- )
#define FORE(a,b,i) for ( i = a ; i <= b ; i++ )
#define FOREB(b,a,i) for ( i = b ; i >= a ; i-- )
#define FORN(a,b,i,nc) for ( i = a ; i < b ; i += nc )
#define FORNB(b,a,i,nc) for ( i = b ; i > a ; i -= nc )
#define FOREN(a,b,i,nc) for ( i = a ; i <= b ; i += nc )
#define FORENB(b,a,i,nc) for ( i = b ; i >= a ; i -= nc )
#define COPY(a,b,lt,rt,i) for ( i = lt ; i <= rt ; i++ ) a[i] = b[i];
#define sqr(a,b) sqrt ( (double) a * a + (double) b * b )
// c++
template < class T> inline T INT_LEN( T v ) { int len = 1 ; while ( v /= 10 ) ++len; return len; }
// #define LocalHost
int M = EDGE;
bool vis[HEAD];
int dis[HEAD] , q[EDGE];
int N;
void spfa ( int * head , re * edge , int s , int M ){ // re 为a边À?的Ì?类¤¨¤型¨ª
int i , u , v , front = 0 , rear = 1 ;
CLEAR ( vis , 0 );
CLEAR ( dis , 0x7f );
dis[s] = 0;
q[ 0] = s;
while ( front != rear ){
u = q[front++] ;
if ( front == M ) front = 0;
vis[u] = false;
for ( i = head[u] ; ~i ; i = edge[i].nxt ){
v = edge[i].v;
if ( dis[v] > dis[u] + edge[i].w ){
dis[v] = dis[u] + edge[i].w ;
if ( !vis[v] ){
vis[v] = true;
if ( dis[v] > dis[q[front]] ){
q[rear++] = v;
if ( rear == M ) rear = 0;
}
else {
if ( --front < 0 ) front = ~-M;
q[front] = v;
}
}
}
}
}
}