边取反,从汇点跑单源最短路即可。

 #include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 1111111
#define MAXM 1111111 inline void in(int &ret){
char c; ret=;
while(c=getchar(),c<''||c>'');
while(c>=''&&c<='') ret=ret*+c-'',c=getchar();
}
void out(long long x){
if(x>) out(x/);
putchar(x%+'');
} struct Edge{
int v,cost,next;
}edge[MAXM<<];
int head[MAXN],rhead[MAXN],NE;
void addEdge(int a,int b,int c){
edge[NE].v=b; edge[NE].cost=c; edge[NE].next=head[a];
head[a]=NE++;
edge[NE].v=a; edge[NE].cost=c; edge[NE].next=rhead[b];
rhead[b]=NE++;
} int n,m;
int d[MAXN];
bool vis[MAXN];
void SPFA(){
memset(d,,sizeof(d));
d[]=;
memset(vis,,sizeof(vis));
vis[]=;
queue<int> que;
que.push();
while(!que.empty()){
int u=que.front(); que.pop();
for(int i=head[u];i !=-; i=edge[i].next){
int v=edge[i].v;
if(d[v]>d[u]+edge[i].cost){
d[v]=d[u]+edge[i].cost;
if(!vis[v]){
vis[v]=;
que.push(v);
}
}
}
vis[u]=;
}
}
void rSPFA(){
memset(d,,sizeof(d));
d[]=;
memset(vis,,sizeof(vis));
vis[]=;
queue<int> que;
que.push();
while(!que.empty()){
int u=que.front(); que.pop();
for(int i=rhead[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(d[v]>d[u]+edge[i].cost){
d[v]=d[u]+edge[i].cost;
if(!vis[v]){
vis[v]=;
que.push(v);
}
}
}
vis[u]=;
}
} int main(){
int t,a,b,c;
in(t);
while(t--){
NE=;
memset(head,-,sizeof(head));
memset(rhead,-,sizeof(rhead));
in(n); in(m);
while(m--){
in(a); in(b); in(c);
addEdge(a,b,c);
}
long long res=;
SPFA();
for(int i=; i<=n; ++i) res+=d[i];
rSPFA();
for(int i=; i<=n; ++i) res+=d[i];
out(res); putchar('\n');
}
return ;
}

POJ1511 Invitation Cards(多源单汇最短路)的更多相关文章

  1. Vijos 1006 晴天小猪历险记之Hill 单源单汇最短路

    背景 在很久很久以前,有一个动物村庄,那里是猪的乐园(^_^),村民们勤劳.勇敢.善良.团结-- 不过有一天,最小的小小猪生病了,而这种病是极其罕见的,因此大家都没有储存这种药物.所以晴天小猪自告奋勇 ...

  2. POJ-1511 Invitation Cards (双向单源最短路)

    Description In the age of television, not many people attend theater performances. Antique Comedians ...

  3. hdoj 2066 一个人的旅行 【多源多汇最短路】

    题目:hdoj 2066 一个人的旅行 方法:缩点 + 最短路 分析:看了大神的一篇博客,讲冗余压缩的,然后就想找一个多源最短路练练手. 这个题目就是典型的多源多汇最短路 方法:把全部的源点压缩成一个 ...

  4. POJ1511:Invitation Cards(最短路)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 34743   Accepted: 114 ...

  5. POJ1511 Invitation Cards —— 最短路spfa

    题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Tota ...

  6. POJ-1511 Invitation Cards( 最短路,spfa )

    题目链接:http://poj.org/problem?id=1511 Description In the age of television, not many people attend the ...

  7. POJ-1511 Invitation Cards (单源最短路+逆向)

    <题目链接> 题目大意: 有向图,求从起点1到每个点的最短路然后再回到起点1的最短路之和. 解题分析: 在求每个点到1点的最短路径时,如果仅仅只是遍历每个点,对它们每一个都进行一次最短路算 ...

  8. POJ-1511 Invitation Cards 往返最短路 邻接表 大量数据下的处理方法

    题目链接:https://cn.vjudge.net/problem/POJ-1511 题意 给出一个图 求从节点1到任意节点的往返路程和 思路 没有考虑稀疏图,上手给了一个Dijsktra(按紫书上 ...

  9. POJ1511 Invitation Cards SPFA算法裸题变形

    原题目:http://poj.org/problem?id=1511 给出一个有向图,求出这个图从1到所有点的最短路径和所有点到1的最短路径的和. 这题数据量有点大,数据范围也大,所以用SPFA+邻接 ...

随机推荐

  1. The Flash

    flash.now[:error] = "" render :new flash[:error] = "" redirect videos_path http: ...

  2. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  3. C#结构体和类的区别

    1.不能在结构体中定义默认构造方法: 2.在结构体中的非默认构造方法中,必须对结构体中所有的字段进行初始化,否则将报错. 3.在类中声明字段的同时,可以初始化,字段的值.在结构体中不可以. 4.结构体 ...

  4. linux awk 内置函数详细介绍(实例)

    这节详细介绍awk内置函数,主要分以下3种类似:算数函数.字符串函数.其它一般函数.时间函数 一.算术函数: 以下算术函数执行与 C 语言中名称相同的子例程相同的操作: 函数名 说明 atan2( y ...

  5. (部署新java程序,程序报错,需copy的一个包)——java使用siger 获取服务器硬件信息

    mcat-siger.sh  查看是否安装siger rsync -aPuv /usr/lib64/libsigar-amd64-linux.so $i:/usr/lib64/ java使用siger ...

  6. SSHPASS支持从命令行输入密码

    参考:http://www.2cto.com/os/201307/227911.html 手动下载地址:http://sourceforge.net/projects/sshpass/ 安装示例: w ...

  7. 【转】maven命令背后是如何工作的

    转载自:http://yinny.iteye.com/blog/1883488 Maven强大的一个重要的原因是它有一个十分完善的生命周期模型(lifecycle),它有三套相互独立的生命周期,请注意 ...

  8. 1.7 逆序数与归并排序[inversion pairs by merge sort]

    [本文链接] http://www.cnblogs.com/hellogiser/p/inversion-pairs-by-merge-sort.html [题目] 编程之美1.7光影切割问题可以进一 ...

  9. ios如何生成crash报告

    #include <signal.h> #include <execinfo.h> void OnProcessExceptionHandler(int sigl) { do ...

  10. codeforces B. Valera and Contest 解题报告

    题目链接:http://codeforces.com/problemset/problem/369/B 题目意思:给出6个整数, n, k, l, r, sall, sk ,需要找出一个满足下列条件的 ...