已知图中从一点到另一点的距离,从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. 【212】HDF更新数据&HDF创建

    HDF更新数据:对原有HDF数据进行数据更新,不破坏HDF的数据结构 pro add_data_sst ;实现将SST增加1度,再将结果更新到SST中 ;1. 获取SST索引 ;2. 通过索引获取ID ...

  2. Moctf--没时间解释了

    记录一道简单的题目. 打开后就张这个样子,,然后看到url为index2.php---->所以我们把它改为index.php(用burp抓包才行,这是一个302跳转). 看到它提示我们要uplo ...

  3. hdoj1596【spfa,松弛】

    积压很久的一道...一看直接spfa水过..但是看那个safest怎么求得?松弛的时候取大. #include <bits/stdc++.h> using namespace std; t ...

  4. combobox级联检索下拉选择框

    1.效果图 2.前端 @{ ViewBag.Title = "Index"; Layout = null; @*自动筛选下拉框*@ <script src="~/S ...

  5. 键值编码 KVC

    http://www.cnblogs.com/dyf520/p/3805297.html 1,什么是Key-Value Coding? Key-Value Coding是一种间接访问对象属性的机制,使 ...

  6. poj3417 闇の連鎖 【树上差分】By cellur925

    闇の連鎖(yam.pas/c/cpp)题目描述传说中的暗之连锁被人们称为 Dark.Dark 是人类内心的黑暗的产物,古今中外的勇者们都试图打倒它.经过研究,你发现 Dark 呈现无向图的结构,图中有 ...

  7. hdu1301 Jungle Roads 基础最小生成树

    #include<iostream> #include<algorithm> using namespace std; ; int n, m; ]; struct node { ...

  8. FlashFXP Registration

    -------- FlashFXP Registration Data START --------FLASHFXPuQBW1wi5uQAAAACvW7cJKQXzmx8Eu6ikXL4LbrYQHZ ...

  9. Building Forest CodeForces - 195E

    Building Forest CodeForces - 195E 这题意真是难懂啊...话说"An oriented weighted forest is an acyclic weigh ...

  10. PWA之push服务

    转载: https://www.jishux.com/p/c5735af96c39bd4a https://www.jianshu.com/p/9970a9340a2d 系列文章参考:https:// ...