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 ...
随机推荐
- Json.Net系列教程 3.Json.Net序列化和反序列化设置
原文 Json.Net系列教程 3.Json.Net序列化和反序列化设置 上节补充 首先补充一点,Json.Net是支持序列化和反序列化DataTable,DataSet,Entity Framewo ...
- BZOJ 2463 谁能赢呢? (博弈论)
题解:简单博弈论 #include <cstdio> int main(){ int n; while(scanf("%d",&n),n!=0) if (n&a ...
- HDU 1787 GCD Again
题目大意:求小于n的gcd(i,n)大于1的个数: 题解:欧拉函数直接求gcd(i,n)==1的个数 用n减即可 #include <cstdio> int eular(int n){ ...
- Structs 2
1 redirect.redirectaction和chain 的区别 当使用type="redirectAction" 或type="redirect"提交 ...
- javascript - C++, Qt, QtWebKit: How to create an html rendering window so that your application would get callbacks from JS calls? - Stack Overflow
javascript - C++, Qt, QtWebKit: How to create an html rendering window so that your application woul ...
- C#后台发送HTTP请求
using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using Syst ...
- git 无法添加文件夹下文件
最近做项目时,发现无法提交某个子文件夹下的文件. google后发现可能是该子文件夹下有.git文件夹导致无法上传. 删除子文件夹下.git后,依然无法提交子文件夹下的文件. 继续google, 尝试 ...
- NSArray的4种遍历方式
前言:NSArray对应的是java的List,不同的是其元素不能更改,不过其派生类NSMutableArray可以更改,遍历的方式跟java的List基本一样 一. for循环 Student * ...
- js去除首尾空格
简单的:str = jQuery.trim(str); var temp = " aa b "; console.log("cc" + temp); temp ...
- Xamarin.Android开发实践(五)
原文:Xamarin.Android开发实践(五) 一.服务的生命周期 服务与活动一样,在它的整个生命周期中存在着一些事件,下图可以很好解释整个过程以及涉及到的方法: 在真实的使用中,Service来 ...