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 ...
随机推荐
- Java并发编程总结3——AQS、ReentrantLock、ReentrantReadWriteLock(转)
本文内容主要总结自<Java并发编程的艺术>第5章——Java中的锁. 一.AQS AbstractQueuedSynchronizer(简称AQS),队列同步器,是用来构建锁或者其他同步 ...
- Beat It
They Told Him他们告诉他: Don't You Ever Come Around Here “你胆敢再来? Don't Wanna See Your Face, 不想再见你, You Be ...
- Oracle 使用RMAN
RMAN 数据库备份 特点: . 跳过未使用的数据块 . 备份压缩 . 执行增量备份 . 块级别的恢复 组件: . RMAN命令执行器(RMAN Executable) . 目标数据库(Traget ...
- oracle 中的select ...connect by prior ...start with 及(+)的用法
1.select ...connect by prior ...start with的用法: select ... from <tablename> where <condition ...
- 【PAT】1009. Product of Polynomials (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1009 分析:简单题.相乘时指数相加,系数相乘即可,输出时按指数从高到低的顺序.注意点:多项式相 ...
- hdu1867之KMP
A + B for you again Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- c 这题做了半天,虽然做好了,但是思路还是不清晰,估计让我再做一次还是比较花时间的。
输入一个大写字符,如F 比如: 输入:F 输出: FEDCBA EDCBAB DCBABC CBABCD BABCDE ABCDEF 输入 B 输出: BA AB #include<stdio. ...
- SQL Server(SSIS package) call .net DLL
There are two method to call .net DLL in SQLSERVER. The first one is to use the sql clr but it has a ...
- C++ Input & Output
1.C++ I/O各类之间的继承关系图 参考网址: http://www.cplusplus.com/reference/iolibrary/ Note: 在程序中包含iostream文件将自动创建8 ...
- 关于JAVA 向上转型
最近复习中比较绕的一个地方 通过试验总结一下 若A为父类 B为子类 声明方式为: A t= new B(); 将子类对象赋值给父类对象,它编译时为父类对象,但运行时却是子类对象: 1)被声明为父类对象 ...