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 ...
随机推荐
- Kali的源得数字验证问题
装上之后第一件事就是执行apt-get update && apt-get upgrade,结果却出现了这样的错误 我添加的是中科大的更新源,在浏览器中是可以正常打开的: deb ht ...
- mktemp---创建暂存文件
- 03007_JDBC概述
1.JDBC概述 (1)JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用J ...
- 【Codeforces Round #427 (Div. 2) B】The number on the board
[Link]:http://codeforces.com/contest/835 [Description] 原本有一个数字x,它的各个数码的和原本是>=k的; 现在这个数字x,在不改变位数的情 ...
- 洛谷 P2021 faebdc玩扑克
P2021 faebdc玩扑克 题目背景 faebdc和zky在玩一个小游戏 题目描述 zky有n个扑克牌,编号从1到n,zky把它排成一个序列,每次把最上方的扑克牌放在牌堆底,然后把下一张扑克牌拿出 ...
- HDU 2563 统计问题 (递推)
A - 统计问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- mac自己定义tree命令
编辑文件: vim ~/.bash_profile 在文件末尾追加: alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____| ...
- 26.event跨进程通信
以id创建事件 ] = "myevent"; HANDLE event = CreateEventA(NULL, FALSE, FALSE, name); 设置事件 SetEven ...
- BZOJ2434: [Noi2011]阿狸的打字机(fail树+dfs序)
Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...
- BZOJ1023: [SHOI2008]cactus仙人掌图(仙人掌)
Description 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌图(cactus).所谓简单回路就是指在图上不重复经过任何一个顶点的 ...