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 ...
随机推荐
- Ubuntu+Django+mod_wsgi+Apache配置过程
Ubuntu15.10 Apache2.4( sudo apt-get install apache2 ) Python3.4( sudo apt-get install apache2 ), [系 ...
- MyBatis学习-SQL 符号篇
当我们需要通过 XML 格式处理 SQL 语句时,经常会用到 <,<=,>,>= 等符号,但是很容易引起 XML 格式的错误,这样会导致后台将 XML 字符串转换为 XML文档 ...
- apt-get 依赖修复
apt-get cleanapt-get update apt-get -f install dpkg --configure -a
- C语言隐式强制类型转换
今天又被精度问题困扰,把最基本的东西忘了. int n = 5; int cnt = 5.5; double sum = (n-cnt); 运算完后sum是 -0.5.不知道什么时候n转换成doub ...
- 用Chrome开发者工具做JavaScript性能分析
来源: http://blog.jobbole.com/31178/ 你的网站正常运转.现在我们来让它运转的更快.网站的性能由页面载入速度和代码执行效率决定.一些服务可以让你的网站载入更快,比如压缩J ...
- SLF4JLoggerContext cannot be cast to LoggerContext
Getting Exception org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.l ...
- myeclipse 调试JSP页面
http://jingyan.baidu.com/article/636f38bb1ef1aad6b9461048.html
- win8.1下安装ubuntu 14.0 4LTS
1.前奏 电脑上已经安装了win8.1系统 2.准备工作 关闭win8.1的快速启动 步骤: 控制面板->电源选项->选择电源按钮的功能->更改不可用的设置,然后把"启用快 ...
- 错误 1 error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 C:\Users\Administ
错误 1 error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 C:\Users\Administ 这两个fatal error是因为从低版本的WTL到高版本的WTL转变后产生的 ...
- Parade
Parade time limit per test 1 second memory limit per test 256 megabytes input standard input output ...