已知图中从一点到另一点的距离,从1号点到另一点再从这一点返回1号点,求去到所有点的距离之和最小值
*解法:正着反着分别建图,把到每个点的距离加起来
spfa跑完之后dist数组就是从起点到每一点的最短距离
get反着建图这种技能
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define SZ 1000005
#define INF 1e18+10
long long dist[SZ], tmp[SZ], dis = ;
int head[SZ], nxt[SZ], tot = ;
int x[SZ], y[SZ];
long long z[SZ];
int use[SZ];
struct edge
{
int t;
long long d;
}l[SZ];
void build(int f, int t, long long d)
{
l[++tot] = (edge){t, d};
nxt[tot] = head[f];
head[f] = tot;
}
queue<int> q;
int p, qq;
void spfa()
{
for(int i = ; i <= p; i++) dist[i] = INF, use[i] = ;
use[] = , dist[] = ;
q.push();
while(q.size())
{
int u = q.front();
q.pop();
use[u] = ;
for(int i = head[u]; i; i = nxt[i])
{
int v = l[i].t;
if(dist[v] > dist[u] + l[i].d)
{
dist[v] = dist[u] + l[i].d;
if(!use[v])
use[v] = , q.push(v);
}
}
}
}
int main()
{
int T, s = ;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &p, &qq);
dis = ;
tot = ;
for(int i = ; i < qq; i++) head[i] = nxt[i] = use[i] = ;
for(int i = ; i < qq; i++)
{
scanf("%d %d %lld", &x[i], &y[i], &z[i]);
build(x[i], y[i], z[i]);
}
spfa();
for(int i = ; i <= p; i++) tmp[i] = dist[i];
tot = ;
for(int i = ; i < qq; i++)
head[i] = nxt[i] = use[i] = ;
for(int i = ; i < qq; i++)
build(y[i], x[i], z[i]);
spfa();
for(int i = ; i <= p; i++)
dis = dis + tmp[i] + dist[i];
printf("%lld\n", dis);
}
return ;
}

最短路 || POJ 1511 Invitation Cards的更多相关文章

  1. POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

  2. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...

  3. poj 1511 Invitation Cards(最短路中等题)

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

  4. poj 1511 Invitation Cards (最短路)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 33435   Accepted: 111 ...

  5. POJ 1511 Invitation Cards (最短路spfa)

    Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...

  6. [POJ] 1511 Invitation Cards

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 18198   Accepted: 596 ...

  7. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  8. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

  9. Poj 1511 Invitation Cards(spfa)

    Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...

随机推荐

  1. kubernetes1.13.1部署ingress-nginx-十一

    一.Ingress 简介 (1) 在Kubernetes中,服务和Pod的IP地址仅可以在集群网络内部使用,对于集群外的应用是不可见的. 为了使外部的应用能够访问集群内的服务, 在Kubernetes ...

  2. Spring boot 启动报错:com.mongodb.MongoSocketOpenException: Exception opening socket

    详细错误信息: com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.connection.Soc ...

  3. 664A - Complicated GCD

    题意真是七零八落,乱七八糟.盲目瞎写,水过就好? #include <cstdio> #include <cstring> #include <algorithm> ...

  4. python __builtins__ int类 (36)

    36.'int', 用于将一个字符串或数字转换为整型 class int(object) | int(x=0) -> integer | int(x, base=10) -> intege ...

  5. 跟我一起玩Win32开发(8):绘图(A)

    从本篇开始,我就不吹牛皮,那就吹吹兔皮吧.说说与绘图有关的东东. 要进行绘制,首先要得到一个DC,啥是DC呢?按字面翻译叫设备上下文,也可以翻译为设备描述表,它主要指API为我们封装了一些与显示设备相 ...

  6. HDU - 6063 RXD and math

    Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6063 打表发现规律是n^k #include <iostream> #inc ...

  7. magento 开启 3D secure credit card validation

    因为国外盗刷严重,于是得开启验证. 首先可以去 https://developer.cardinalcommerce.com/try-it-now.shtml.这上面有测试账号,截图如下:

  8. iOS UILabel UITextView自适应文本,或文本大小自适应

    //UILabel自适应文本的高度 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(, , , )]; label.numberOf ...

  9. 创建表规范 lob 字段

    ORAClce 11g 提供如下特性: BasicfileOracle10g 及之前版本被称为basicfile Securefile11g中新增securefile 优点:集中写入缓存(WGC),4 ...

  10. Android中ProgressBar显示小数的方法

    Android原生的ProgressBar的ProgressDialog.STYLE_HORIZONTAL(即水平样式)默认setMax和setProgress只能传int型的参数,而实际项目中我需要 ...