【反向建立边】【spfa】【仿vector设计】





#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
    
#define rep(i,a,n) for (int i=a;i=a;i--)
#define mp push_back

int T,n,m;
int H[1000100];
int x[1000100];
int y[1000100];
int z[1000100];
long long d[1000100];
int top;
struct Node
{
	int v,c,next;
}node[1000100];

void addedge(int u,int v,int c)
{
	node[top].v = v;
	node[top].c = c;
	node[top].next = H[u];

	H[u] = top++;
}

long long spfa(int s)
{
   queue Q;
   Q.push(s);
   memset(d,0x3f3f3f3f,sizeof(d));
   d[s] = 0;

   while(!Q.empty())
   {
	   int cur = Q.front();
	   Q.pop();
	   for(int i=H[cur];i!=-1;i=node[i].next)
	   {
		   if(d[node[i].v] > d[cur] + node[i].c)
		   {
			  d[node[i].v] = d[cur] + node[i].c;
			  Q.push(node[i].v);
		   }
	   }
   }
   int mins = 0x3f3f3f3f;
   long long ret = 0;
   rep(i,1,n+1) ret += d[i];
   return ret;
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		memset(H,-1,sizeof(H));
		scanf("%d%d",&n,&m);
		top = 0;
		for(int i=0;i


你可能感兴趣的:(ACM_template,ACM_图论)