POJ 1511 Invitation Cards (ZOJ 2008) 使用优先队列的dijkstra
传送门:
http://poj.org/problem?id=1511
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1008
题目大意:
给定p个点,还有他们的边q,(有向图)求从结点1出发到所有结点和所有结点到1的最短路径之和。
其中1 <= P,Q <= 1000000
思路:
和上次 POJ 3268 Silver Cow Party 一样,有向图倒着建立就把从所有结点到1的最短路径改为了从1到所有,那么只需两次dijkstra即可。
还有就是数据量大,要用优先队列的dijkstra,堆优化的更好。
嗯,第一次写这个,参考了别人的。
code1:2014/1/1更新。。。
今天自己写的------
直接用数组模拟链表快了好多,zoj排名挺靠前面的,嘻嘻,不过poj又被虐。。
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=1000000+10;
bool vis[MAXN];
int head[2][MAXN];
int n,m;
long long ans;
struct edge
{
int to;
int cost;
int next;
}e[2][MAXN]; struct node
{
int from;
int cost;
node(int f,int c){from=f;cost=c;}
bool operator < (const node& b)const
{
return cost > b.cost;
}
}; int len;
void insert(int from,int to,int cost)
{
e[0][len].to=to;
e[1][len].to=from; e[0][len].cost=e[1][len].cost=cost; e[0][len].next = head[0][ from ];
e[1][len].next = head[1][ to ]; head[0][from]=len;
head[1][to]=len; len++;
} void dijkstra(int kind)
{
memset(vis,0,sizeof(vis));
priority_queue<node> q;
q.push(node(1,0)); int num=0;
while(!q.empty())
{
node cur=q.top();
q.pop(); if(vis[cur.from]==true) continue; vis[cur.from]=true;
ans+=cur.cost;
num++; if(num==n)
break; //这里写得真纠结- -||| for(int ne=head[kind][cur.from];ne!=-1;ne=e[kind][ ne ].next)
{
if(!vis[e[kind][ ne ].to])
q.push(node(e[kind][ ne ].to,e[kind][ ne ].cost+cur.cost));
}
}
} int main()
{
int T;
scanf("%d",&T);
int from,to,cost;
while(T--)
{
len=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&from,&to,&cost);
insert(from,to,cost);
}
ans=0;
dijkstra(0);
dijkstra(1);
printf("%lld\n",ans);
}
return 0;
}
code 2:
2013/12/31第一次写,基本上是模仿别人的
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=1000000+10;
long long ans;
bool vis[MAXN];
int n,m;
struct edge
{
int to,cost;
edge* next;
edge(){next=NULL;}
}*e[2][MAXN]; struct node
{
int cost,id;
bool operator < (const node& b) const
{
return cost>b.cost;
}
node(int i,int c){ cost =c;id=i;}
}; inline void addedge(int from,int to,int cost)
{
edge *p=new edge;
edge *q=new edge; p->next=e[0][from];
q->next=e[1][to]; p->to=to;
q->to=from; p->cost=q->cost=cost; e[0][from]=p;
e[1][to]=q;
} void dijkstra(int kind)
{
memset(vis,0,sizeof(vis));
priority_queue<node> q;
int num=0;
node cur(1,0);
q.push(cur);
while(!q.empty())
{
cur=q.top();
q.pop();
if(vis[cur.id]==true)
continue; vis[cur.id]=true;
ans+=cur.cost;
num++;
if(num==n)
break; for(edge *p=e[kind][cur.id];p!=NULL;p=p->next)
{
if(vis[p->to]==false)
{
node temp(p->to,p->cost+cur.cost);
q.push(temp);
}
}
} } int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(e,NULL,sizeof(e)); scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int from,to,cost;
scanf("%d%d%d",&from,&to,&cost);
addedge(from,to,cost);
}
ans=0;
dijkstra(0);
dijkstra(1);
printf("%lld\n",ans);
}
return 0;
}
POJ 1511 Invitation Cards (ZOJ 2008) 使用优先队列的dijkstra的更多相关文章
- 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(单源最短路,优先队列优化的Dijkstra)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 16178 Accepted: 526 ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- 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 题目链接: 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。
Description In the age of television, not many people attend theater performances. Antique Comedians ...
- POJ 1511 Invitation Cards 链式前向星+spfa+反向建边
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 27200 Accepted: 902 ...
随机推荐
- 今日题解------codeforce 893d
题意:给你一个数列,小于零表示表示信用卡里取出钱,大于零表示信用卡里存钱,等于零表示要查询信用卡, 如果被查到信用卡里的钱小于零,那你就GG,或者在任何时候你的信用卡里的钱大于d的话(不需要找ai等于 ...
- 【Cocos2d-x 017】 多分辨率适配全然解析
转:http://blog.csdn.net/w18767104183/article/details/22668739 文件夹从Cocos2d-x 2.0.4開始,Cocos2d-x提出了自己的多分 ...
- 嵌入式 Linux应用程序如何读取(修改)芯片寄存器的值
这一问题来自项目中一个实际的需求:我需要在Linux启动之后,确认我指定的芯片寄存器是否与我在uboot的配置一致. 举个例子:寄存器地址:0x20000010负责对DDR2的时序配置,该寄存器是在u ...
- Android 打造属于自己的RxBus
RxBus 通过RxJava实现Rxbus. 相信大家已经非常熟悉EventBus了.最近正在学习Rxjava,如果在项目中已经使用了Rxjava,使用RxBus来代替EventBus应该是不错的选择 ...
- golang标准包中文手册
golang标准包中文手册 http://files.cnblogs.com/files/rojas/liudiwu-pkgdoc-master.zip
- POJ Fence Repair(优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 51346 Accepted: 16857 De ...
- 基于面向对象js的弹窗的组件的开发案例
var aInput = document.getElementsByTagName("input"); 2 aInput[0].onclick = function() { 3 ...
- Maven学习笔记4
POM解析pom描述自身坐标,以及它关联的依赖,插件,仓库等. 项目模块设计项目聚合和继承. pom标签解析1. pom标签解析2. pom标签解析3. pom标签解析4. 聚合和继承 聚合模块只保留 ...
- 推广一下新Blog www.hrwhisper.me
新博客地址:www.hrwhisper.me 欢迎互访加友链~
- 使用PyCharm安装第三方库
使用PyCharm安装第三方库是一种十分简单的做法,接下来我来演示一下在PyCharm上安装第三方库requess的操作流程. 首先,先看一下当第三方库未安装时的提示内容,在pycharm中新建pyt ...