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

 #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. String的内存分配

    1.String类是final类不能被继承 2.String str="abc"的内部工作 (1)先在栈中定 一个名为str的String类的引用变量 String str: (2 ...

  2. 他们在军训,我在搞 OI(四)

    (怎么自动变成两天一更了?) ——因为我菜啊 T_T Day 5 今天上午刷得爽啊!5 道 NOIP,前四题直接 1A,然而最后一题还是 WA 了一发才 A... 第一题是个简单的贪心,题意大概是 n ...

  3. nginx学习(一):基本安装

    转载自http://summervast.blog.51cto.com/690507/385511 注意:可能因版本不同,个别指令不起作用,需要注意版本灵活安装,我在安装时也遇到过此问题 开始学习ng ...

  4. neutron 中 flat vlan gre vxlan的区别

    In a flat network, everyone shares the same network segment. For example, say 2 tenants are sharing ...

  5. 【持续集成】使用Jenkins实现多平台并行集成

    使用Jenkins实现多平台并行集成 二月 15, 2012 暂无评论 我们的后端C应用都是支持跨平台的,至少目前在Linux和Solaris上运行是没有问题的,这样一来我们在配置持续集成环境时就要考 ...

  6. Redis windows安装配置与Jedis访问数据库

    一 Redis概要 Redis是一个开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.它通常被称为数据结构服务器 ...

  7. Hydra---Linux下的暴力美学

    引自:http://www.cnblogs.com/mchina/archive/2013/01/01/2840815.html 安装:http://www.91ri.org/2867.html yu ...

  8. 【转】Mybatis/Ibatis,数据库操作的返回值

    该问题,我百度了下,根本没发现什么有价值的文章:还是看源代码(详见最后附录)中的注释,最有效了!insert,返回值是:新插入行的主键(primary key):需要包含<selectKey&g ...

  9. python动态获取对象的属性和方法

    http://blog.csdn.net/kenkywu/article/details/6822220首先通过一个例子来看一下本文中可能用到的对象和相关概念.01     #coding: UTF- ...

  10. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...