题意:从1点到所有点的来回最短路的和
解法:链式向前星(书上就是这术语)+SPFA
*注意要把所有的数组开在函数的外面,因为这个一直WA
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <iostream>
#define maxn 1000005
#define ull unsigned long long
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
using namespace std;
const ull inf =1000000005;
using namespace std;
int head[maxn];
struct edge
{
int to;
int val;
int next;
}edges[maxn];
int be[maxn];
int en[maxn];
int val[maxn];
int used[maxn];
int ip;//边的个数
int n,m;//点和边的个数
int dis[maxn];
queue<int> q;
void ins(int from,int to,int val)
{
edges[++ip].to=to;
edges[ip].val=val;
edges[ip].next=head[from];
head[from]=ip;
}
void spfa()
{
int i,j,k;
cle(used);
int top;
for(i=0;i<=n;i++)
{
dis[i]=inf;
}
q.push(1);
used[1]=1;
dis[1]=0;
while(!q.empty())
{
top=q.front();
q.pop();
k=head[top];
while(k>0)
{
if(dis[edges[k].to]>dis[top]+edges[k].val)
{
dis[edges[k].to]=dis[top]+edges[k].val;
if(!used[edges[k].to])
{
used[edges[k].to]=1;
q.push(edges[k].to);
}
}
k=edges[k].next;
}
}
}
int main()
{
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
cin>>T;
// cout<<T<<endl;
while(T--)
{
int i,j,k;
cle(head);ip=0;
scanf("%d%d",&n,&m);
rep(i,m)
{
scanf("%d%d%d",&be[i],&en[i],&val[i]);
ins(be[i],en[i],val[i]);
}
spfa();
long long ans=0;
for(i=1;i<=n;i++)
{
// cout<<dis[i]<<endl;
ans+=dis[i];
}
ip=0;cle(head);
rep(i,m)
{
ins(en[i],be[i],val[i]);
}
spfa();
for(i=1;i<=n;i++)
{
ans+=dis[i];
}
printf("%I64d\n",ans);
}
return 0;
}