题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1535

分析:

题意:求1点到其它点的最短距离之和+其它点到1点的最短距离之和

前面一部分直接用SPFA算法求出,而后一部分可用一数组存放反向边

(所有边的方向都反一下),利用反向边SPFA求出1点到其它点距离即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std;
const int inf = 0xfffffff;
const int maxn = 1000000+10; bool vis[maxn];
int h1[maxn],h2[maxn],dis[maxn];
int t1,t2,n,m; struct node{
int x,v,next;
}f1[maxn],f2[maxn];
///f1存放顺向边, f2存放反向边 void init(){
t1=t2=0;
memset(h1,-1,sizeof(h1));
memset(h2,-1,sizeof(h2));
}
void addnode_1(int a,int b,int c){
f1[t1].x=b;
f1[t1].v=c;
f1[t1].next=h1[a];
h1[a]=t1++;
}
void addnode_2(int a,int b,int c){
f2[t2].x=b;
f2[t2].v=c;
f2[t2].next=h2[a];
h2[a]=t2++;
} int spfa(node F[ ],int H[ ]){
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;++i)
dis[i]=inf;
dis[1]=0;
vis[1]=true;
queue<int>M;
M.push(1);
while(!M.empty()){
int now=M.front(); M.pop();
vis[now]=false;
for(int i=H[now];i!=-1;i=F[i].next){
int next=F[i].x;
if(dis[next]>dis[now]+F[i].v){
dis[next]=dis[now]+F[i].v;
if(!vis[next]){
vis[next]=true;
M.push(next);
}
}
}
}
int sum=0;
for(int i=2;i<=n;++i)
sum+=dis[i];
return sum;
} int main(){
int T; scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
while(m--){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
addnode_1(a,b,c);
addnode_2(b,a,c);
}
int ans=spfa(f1,h1)+spfa(f2,h2);
cout<<ans<<endl;
}
return 0;
}

HDU SPFA算法 Invitation Cards的更多相关文章

  1. (最短路 SPFA)Invitation Cards -- poj -- 1511

    链接: http://poj.org/problem?id=1511 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82829#probl ...

  2. HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...

  3. hdu 1535 Invitation Cards(spfa)

    Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  4. HDU 1535 Invitation Cards(最短路 spfa)

    题目链接: 传送门 Invitation Cards Time Limit: 5000MS     Memory Limit: 32768 K Description In the age of te ...

  5. hdu 1535 Invitation Cards(SPFA)

    Invitation Cards Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) T ...

  6. SPFA算法(2) POJ 1511 Invitation Cards

    原题: Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 31230   Accepted: ...

  7. HDU - 1535 Invitation Cards 前向星SPFA

    Invitation Cards In the age of television, not many people attend theater performances. Antique Come ...

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

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

  9. HDU1535——Invitation Cards(最短路径:SPAF算法+dijkstra算法)

    Invitation Cards DescriptionIn the age of television, not many people attend theater performances. A ...

随机推荐

  1. RADOS工作原理

    转:http://www.csdn.net/article/2014-04-08/2819192-ceph-swift-on-openstack-m/2 Ceph的工作原理及流程 本节将对Ceph的工 ...

  2. Tableau 群集部署

    由于公司连续两个月月底Tableau服务器过载崩溃,因此有了搭建Tableau服务器群集的想法.目前还在测试阶段,所以做一步写一步了. 目录: 一.安装和配置工作服务器 二.其他参考文档 一. 安装和 ...

  3. Oracle递归sql笔记

    查询一个机构下所辖机构: select * from t00_organ t start with t.organkey=#uporgankey# connect by prior t.organke ...

  4. KMP算法的一个C++实现

    本文参考阮一峰老师的KMP算法,重点是“部分匹配表”的建立.算法可参考 http://kb.cnblogs.com/page/176818/ . /* * kmp.cpp * Author: Qian ...

  5. PHP学习建议(来自老手)

    框架太多了,有一个用着,先用熟练,因为框架思想区别不大. 用熟悉一个,再看其他,就容易多.看那么多,没有一个熟悉的,还是什么也不知道. 框架还是要用熟悉才行,然后才是产品如何设计,mysql性能真的有 ...

  6. Yii框架中的CURD操作

    <?php $Admin = new Admin(); //查找多条记录,返回二维数组 $Admin->findAll(); $Admin->findAll("id = 2 ...

  7. commons-logging \ log4j \ slf4j 之间的关系

    最近的一个web项目中要使用到日志,但是对常用的日志记录工具(框架)着实不是很理解,在此mark一下. 1.commons-logging.jar common-logging是apache提供的一个 ...

  8. 腾讯地图之Marker

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. CQRS架构设计及其实现

    CQRS架构设计及其实现 一.为什么要实践领域驱动? 近一年时间我一直在思考一个问题:“如何设计一个松耦合.高伸缩性.易于维护的架构?”.之所以有这样的想法是因为我接触的不少项目都是以数据库脚本来实现 ...

  10. StringIO模块字符串的缓存

    StringIO经常被用来作为字符串的缓存,应为StringIO有个好处,他的有些接口和文件操作是一致的,也就是说用同样的代码,可以同时当成文件操作或者StringIO操作.比如: import st ...