题目链接: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. 文本相似度算法——空间向量模型的余弦算法和TF-IDF

    1.信息检索中的重要发明TF-IDF TF-IDF是一种统计方法,TF-IDF的主要思想是,如果某个词或短语在一篇文章中出现的频率TF高,并且在其他文章中很少出现,则认为此词或者短语具有很好的类别区分 ...

  2. ASP.NET页面传值的几种方式

    页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值. 存储对象传值.ajax.类.model.表单等!下面欧柏泰克和大家一起来看看asp.net页面传值方式一般有哪些?常用的较简单 ...

  3. sign a third-party dll which don't have a strong name

    Problem: Assembly generation failed -- Referenced assembly '' does not have a strong name Cause: thi ...

  4. C#面向对象编程基础-喜课堂笔记

    **************[5][C#面向对象编程基础]第1讲:类与对象****************                 *************2.1.1_类与对象的概念**** ...

  5. windows cmd: 打开windows系统程序或服务的常见命令

    Windows常用CMD命令 http://www.cnblogs.com/sbaicl/archive/2013/03/05/2944001.html 其实查找Windows自带程序的命令行很简单, ...

  6. C#常用控件缩写

  7. [C#参考]委托机制

    1. 委托概述 这是一个新的概念,但是其本质并不是什么新鲜的事物,委托本质上就是一个类.只不过一般的类是数据的集合,委托保存的是一个或者多个方法.委托是引用类型,因此委托有引用和对象,同时委托对象中包 ...

  8. MongoDB Linux下的安装和启动(转)

    1. 下载MongoDB,此处下载的版本是:mongodb-linux-i686-1.8.1.tgz.tar. http://fastdl.mongodb.org/linux/mongodb-linu ...

  9. PHP自练项目之发送短信内容

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. c++设计模式总结 好久没写博客了 实在是忙

    具体代码就不贴出来了   通俗易懂的理解方式      原创 c++设计模式: 简单工厂模式 工厂模式有一种非常形象的描述,建立对象的类就如一个工厂,而需要被建立的对象就是一个个产品:在工厂中加工产品 ...