HDU SPFA算法 Invitation Cards
题目链接: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的更多相关文章
- (最短路 SPFA)Invitation Cards -- poj -- 1511
链接: http://poj.org/problem?id=1511 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82829#probl ...
- HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...
- hdu 1535 Invitation Cards(spfa)
Invitation Cards Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 1535 Invitation Cards(最短路 spfa)
题目链接: 传送门 Invitation Cards Time Limit: 5000MS Memory Limit: 32768 K Description In the age of te ...
- hdu 1535 Invitation Cards(SPFA)
Invitation Cards Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other) T ...
- SPFA算法(2) POJ 1511 Invitation Cards
原题: Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 31230 Accepted: ...
- HDU - 1535 Invitation Cards 前向星SPFA
Invitation Cards In the age of television, not many people attend theater performances. Antique Come ...
- POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- HDU1535——Invitation Cards(最短路径:SPAF算法+dijkstra算法)
Invitation Cards DescriptionIn the age of television, not many people attend theater performances. A ...
随机推荐
- Windows 8.1 IIS 8.5 远程管理 Windows 2008 R2 IIS 7.0
案例: Windows 8.1 x64 IIS 8.5 inetmgr_amd64_v1.1_en-US.msi Windows 2008 R2 x64 IIS 7.0 在Win8.1 通过IIS ...
- utf8_general_ci 、utf8_general_cs和utf8_bin的区别
用了这么长时间,发现自己竟然不知道utf_bin和utf_general_ci这两者到底有什么区别..ci是 case insensitive, 即 "大小写不敏感", a 和 A ...
- struts2自己定义类型转换器
1.1. struts2自己定义类型转换器 1) 自定类型转换类,继承DefaultTypeConverter类 package com.morris.ticket.conversio ...
- CSS+DIV标签命名规范 搜索引擎最喜欢
搜索引擎优化(seo)有很多工作要做,其中对代码的优化是一个很关键的步骤.为了更加符合SEO的规范,下面是目前流行的CSS+DIV的命名规则: 登录条:loginBar 标志:logo 侧栏:si ...
- authorization 元素(ASP.NET 设置架构)
authorization 元素(ASP.NET 设置架构) 其他版本 1(共 1)对本文的评价是有帮助 - 评价此主题 [本文档仅供预览,在以后的发行版中可能会发生更改.包含的空白主题用作占位符.] ...
- javaweb学习路之四--cxf发布Webservice
背景:maven构建的springMvc+mybatis框架 源码--->https://github.com/Zering/MyWeb 步骤:(本步骤是自己在实际探索过程中的步骤,我的思路是先 ...
- 实现AJAX局部刷新以及PageMethod方法的使用
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...
- HDU 1222(数论,最大公约数)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descr ...
- STL之vector(向量)
C++编程语言中有一种叫做Vector的应用方法,它的作用在实际编程中是非常重要的,这里详细介绍一下C++ Vector的相关应用技巧及基本内容: Construct vector #include ...
- 使用JS进行pc端、手机端判断
<script type="text/javascript"> (function(){ var ua = nav ...