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 ...
随机推荐
- c语言-扑克牌小魔术
/************************************* Copyright(C) 2004-2005 vision,math,NJU. File Name: guess_card ...
- hdu_2089_不要62(数位DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:中文,不解释 题解:dp[i][j]表示当前第i位的前一个数为j,然后记忆化dfs,注意的 ...
- 监控 monitor java 代码
/* * To change this license header, choose License Headers in Project Properties. * To change this t ...
- oracle添加sequence
CREATE SEQUENCE seq_tm_function INCREMENT BY 1 -- 每次加几个 START WITH 100000015 -- 从1开始计数 NOMAXVALUE -- ...
- 深入理解typedef
首先请看看下面这两句: typedef int a[10]; typedef void (*p)(void); 如果你能一眼就看出它 ...
- Linux 查硬件配置
一:查看cpu more /proc/cpuinfo | grep "model name" grep "model name" /proc/cpuinfo 如 ...
- vs 发布web应用程序时,找不到cs文件错误
将*.aspx.*.ascx.*.master所有出错页面文件中的 CodeFile="******.aspx.cs" 批量替换成 Codebehind="******. ...
- byte数组转16进制 输出到文件
try { File file = new File(Environment.getExternalStorageDirectory(),"shuju2"); if(!file.e ...
- sinaBlog中小知识总结
1.上拉刷新的时候记得移除老的数据(同样应用于其他地方) if (vc.currentPage == 1) { //上拉加载 记得移除新的 [vc.totalArr removeAllObjects] ...
- php并发控制 , 乐观锁
由于悲观锁在开始读取时即开始锁定,因此在并发访问较大的情况下性能会变差.对MySQL Inodb来说,通过指定明确主键方式查找数据会单行锁定,而查询范围操作或者非主键操作将会锁表. 接下来,我们看一下 ...