POJ 1511 Invitation Cards 正反SPFA
题意:学生从A站到B站花费C元,将学生每天从‘1’号站送往其它所有站,再从所有站接回到‘1’号站,问着个过程最小花费是多少。
思路:因为数据很大所以要用SPFA,因为不仅要从1点连接各个点还要从各个点返回一点,所以需要正邻接表和逆邻接表。然后正反各跑一次SPFA,值得注意的是因为数据很大,要将INF定位0xffffffff。
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<string>
#include<math.h>
#include<queue>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<cmath> #define INF 0xffffffff
#define MAX 1000005 using namespace std; int vis[MAX],a[MAX],b[MAX],k,n,m; long long dist[MAX]; struct node
{
int u,v,nextgo,nextback;
long long w;
} Map[MAX]; void Init()
{
int i,j; memset(vis,,sizeof(vis)); for(i=; i<MAX; i++)
{
a[i]=-;
b[i]=-;
dist[i]=INF*;
}
} void Add(int u,int v,int w)
{
Map[k].u=u;
Map[k].v=v;
Map[k].w=w;
Map[k].nextgo=a[u];//正向建图
Map[k].nextback=b[v];//逆向建图
a[u]=k;
b[v]=k++;
} long long gospfa()
{
long long sum=; queue<int>Q;
int start=,i;
vis[start]=;
dist[start]=;
Q.push(start); while(!Q.empty())
{
start=Q.front();
Q.pop();
vis[start]=; for(i=a[start]; i!=-; i=Map[i].nextgo)
{
int v=Map[i].v;
if(dist[v] > dist[start]+Map[i].w)
{
dist[v]=dist[start]+Map[i].w; if(!vis[v])
{
vis[v]=;
Q.push(v);
}
}
}
} for(i=; i<=n; i++)
{
sum+=dist[i];
} return sum;
} long long backspfa()
{
long long sum=; queue<int>Q;
int start=,i;
vis[start]=;
dist[start]=;
Q.push(start); while(!Q.empty())
{
start=Q.front();
Q.pop();
vis[start]=; for(i=b[start]; i!=-; i=Map[i].nextback)
{
int u=Map[i].u;
if(dist[u] > dist[start]+Map[i].w)
{
dist[u]=dist[start]+Map[i].w; if(!vis[u])
{
vis[u]=;
Q.push(u);
}
}
}
} for(i=; i<=n; i++)
{
sum+=dist[i];
} return sum;
} int main()
{
int T,i,j,u,v;
long long w,ans; scanf("%d",&T); while(T--)
{
ans=;
k=; scanf("%d%d",&n,&m); Init(); for(i=; i<=m; i++)
{
scanf("%d%d%lld",&u,&v,&w); Add(u,v,w);
} ans+=gospfa(); for(i=;i<MAX;i++)
dist[i]=INF;
memset(vis,,sizeof(vis)); ans+=backspfa(); printf("%lld\n",ans);
} return ;
}
POJ 1511 Invitation Cards 正反SPFA的更多相关文章
- (简单) POJ 1511 Invitation Cards,SPFA。
Description In the age of television, not many people attend theater performances. Antique Comedians ...
- POJ 1511 Invitation Cards(逆向思维 SPFA)
Description In the age of television, not many people attend theater performances. Antique Comedians ...
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
- POJ 1511 Invitation Cards (最短路spfa)
Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- POJ 1511 Invitation Cards 链式前向星+spfa+反向建边
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 27200 Accepted: 902 ...
- SPFA算法(2) POJ 1511 Invitation Cards
原题: Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 31230 Accepted: ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
随机推荐
- java并发编程框架 Executor ExecutorService invokeall
首先介绍两个重要的接口,Executor和ExecutorService,定义如下: public interface Executor { void execute(Runnable command ...
- Chapter 2 Open Book——10
I sent that, and began again. 我发送了它,然后又一次重新开始写了. Mom,Everything is great. Of course it's raining. I ...
- 在Java中Arrays工具类实现功能的六种方法
使用Arrays工具类,要先导入包即:import.java.util.Arrays 以下是实现六种功能的方法: 1.比较两个数组值是否相等: 结果为true.false.(布尔型不能比较) int ...
- openwrt ramips随记
ar71xx / brcm47xx / brcm63xx / ramips是指cpu的系列,ramips是指ralink系列的
- nefu 753 n!末尾有多少个0
Problem : 753 Time Limit : 1000ms Memory Limit : 65536K description 计算N!末尾有多少个0 input 输入数据有多组,每组1行,每 ...
- a/b + c/d
a/b + c/d Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- JavaScript性能优化技巧之函数节流
函数节流技术的主要思路是,通过一个定时器,阻断连续重复的函数调用.对于我们自己内部使用的函数,这通常意义不大,也不推荐使用这个技术,它可能会丢失对某些数据的处理.但是对于在用户界面调用的函数,却非常有 ...
- Eclipse 安装 HDFS 插件
Eclipse 安装 hdfs 连接插件 1.插件安装 在$HADOOP_HOME/contrib/eclipse-plugin/文件夹中有个hadoop-eclipse-plugin-0.20.20 ...
- Ansible4:Ad-hoc与命令执行模块【转】
Ad-Hoc 是指ansible下临时执行的一条命令,并且不需要保存的命令,对于复杂的命令会使用playbook.Ad-hoc的执行依赖于模块,ansible官方提供了大量的模块. 如:command ...
- sscanf用法
sscanf与scanf类似,都是用于输入的,只是后者以键盘(stdin)为输入源,前者以固定字符串为输入源. 1. 常见用法. 1 2 3 char buf[512] ; sscanf(" ...