#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
typedef pairPII;
const int maxn=1000010;
const LL INF=1e10+10;
struct Edge{
int to;
LL weight;
Edge(int to,LL weight):to(to),weight(weight) {}
};
vectoredges[maxn];
int u[maxn],pos[maxn]; //反向建图
LL cost[maxn];
LL d[maxn];
bool done[maxn];
int v,e;
void dijkstra()
{
memset(done,false,sizeof(done));
for(int i=1;i<=v;i++)
d[i]=(i==1?0:INF);
priority_queuevector,greater >pq;
pq.push(PII(d[1],1));
while(!pq.empty())
{
PII k=pq.top();
pq.pop();
int v1=k.second;
if(done[v1]) continue;
done[v1]=true;
for(int i=0;iif(d[edges[v1][i].to]>d[v1]+edges[v1][i].weight) {
d[edges[v1][i].to]=d[v1]+edges[v1][i].weight;
pq.push(PII(d[edges[v1][i].to],edges[v1][i].to));
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&v,&e);
for(int i=0;iscanf("%d%d%lld",&u[i],&pos[i],&cost[i]);
edges[u[i]].push_back(Edge(pos[i],cost[i]));
}
dijkstra();
LL sum=0;
for(int i=1;i<=v;i++)
{
sum+=d[i];
edges[i].clear();
}
for(int i=0;ifor(int i=1;i<=v;i++)
{
sum+=d[i];
edges[i].clear();
}
printf("%lld\n",sum);
}
return 0;
}
//链式前向星+dijkstra
//dijkstra,有向图,逆邻接表
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
typedef pairPII;
const int maxn=1000010;
const LL INF=1e10+10;
struct Edge{
int next,to;
LL weight;
};
Edge edges[maxn];
int head[maxn],cnt;
int u[maxn],pos[maxn];
LL cost[maxn];
LL d[maxn];
bool done[maxn];
int v,e;
void dijkstra()
{
memset(done,false,sizeof(done));
for(int i=1;i<=v;i++)
d[i]=(i==1?0:INF);
priority_queue,greater >pq;
pq.push(PII(d[1],1));
while(!pq.empty())
{
PII k=pq.top();
pq.pop();
int v1=k.second;
if(done[v1]) continue;
done[v1]=true;
for(int i=head[v1];i!=0;i=edges[i].next)
if(d[edges[i].to]>d[v1]+edges[i].weight) {
d[edges[i].to]=d[v1]+edges[i].weight;
pq.push(PII(d[edges[i].to],edges[i].to));
}
}
}
void add(int U,int V,LL c)
{
edges[++cnt].next=head[U];
edges[cnt].to=V;
edges[cnt].weight=c;
head[U]=cnt;
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
int T;
scanf("%d",&T);
while(T--)
{
memset(head,0,sizeof(head));
cnt=0;
scanf("%d%d",&v,&e);
for(int i=0;i