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 ...
 
随机推荐
- hdoj 1269 迷宫城堡(强连通分量)
			
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1269 思路分析:该问题要求判断是否每两个房间都可以相互到达,即求该有向图中的所有点是否只构成一个强连通 ...
 - Book of Evil 树双向DFS
			
Book of Evil Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area ...
 - DBV-00111: OCI failure (3722) (ORA-01002: fetch out of sequence解决
			
在使用DBV检测segment的时候出现 DBV-00111: OCI failure (3722) (ORA-01002: fetch out of sequence)错误: 在寻找原因过程中发现相 ...
 - x0vncserver Fatal server error: no screens found
			
I make a connection through SSH and then I type: # x0vncserver --PasswordFile=/home/hello/.vnc/pass ...
 - 3.5 用NPOI操作EXCEL--巧妙使用Excel Chart
			
在NPOI中,本身并不支持Chart等高级对象的创建,但通过l模板的方式可以巧妙地利用Excel强大的透视和图表功能,请看以下例子. 首先建立模板文件,定义两列以及指向此区域的名称“sales”: 创 ...
 - 利用GDataXML解析XML文件
			
1.导入GDataXMLNode.h 和 GDataXMLNode.m文件 2.导入libxml2库文件 3.工程target下Bulid Settings 搜索search 找到Hearder S ...
 - photoshop使用注意事项
			
CMYK 与 RGB 任何网络图片都会以RGB模式显示图片: 数码图片以RGB模式被捕捉,因此应在RGB模式下编辑: 大部分工具和滤镜只能在RGB模式下使用: RGB模式和CMYK模式之间不能实现无损 ...
 - Piggy-Bank (完全背包)
			
Description Before ACM can do anything, a budget must be prepared and the necessary financial supp ...
 - Linux下Apache服务器并发优化
			
Linux/UnixLinux系统下Apache 并发数的优化 Apache Http服务器采用prefork或者是worker两种并发控制模式. preforkMPM 使用多个子进程,每个子进程只 ...
 - Sort list by merge sort
			
使用归并排序对链表进行排序 O(nlgn) 的时间效率 /** * Definition for singly-linked list. * struct ListNode { * int val; ...