传送门:

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的更多相关文章

  1. 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 / ...

  2. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...

  3. [POJ] 1511 Invitation Cards

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 18198   Accepted: 596 ...

  4. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  5. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

  6. POJ 1511 Invitation Cards (最短路spfa)

    Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...

  7. Poj 1511 Invitation Cards(spfa)

    Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...

  8. (简单) POJ 1511 Invitation Cards,SPFA。

    Description In the age of television, not many people attend theater performances. Antique Comedians ...

  9. POJ 1511 Invitation Cards 链式前向星+spfa+反向建边

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 27200   Accepted: 902 ...

随机推荐

  1. Kinect 开发 —— 语音识别(上)

    Kinect的麦克风阵列在Kinect设备的下方.这一阵列由4个独立的水平分布在Kinect下方的麦克风组成.虽然每一个麦克风都捕获相同的音频信号,但是组成阵列可以探测到声音的来源方向.使得能够用来识 ...

  2. 小米开源文件管理器MiCodeFileExplorer-源码研究(2)-2个单实例工具类

    从本篇开始,讲解net.micode.fileexplorer.util工具包中的类.这个包下的类,功能也比较单一和独立.很多代码的思想和实现,可以用于JavaWeb和Android等多种环境中. 一 ...

  3. 一个简单RPC框架是怎样炼成的(II)——制定RPC消息

    开局篇我们说了,RPC框架的四个核心内容 RPC数据的传输. RPC消息 协议 RPC服务注冊 RPC消息处理 以下,我们先看一个普通的过程调用 class Client(object): def _ ...

  4. highcharts 阶梯图表并填充颜色(自己觉得:直角折线图表)

    例如以下:普通阶梯图 $(function () { $('#container').highcharts({         title: {         text: '普通阶梯图'       ...

  5. C. Arthur and Table(Codeforces Round #311 (Div. 2) 贪心)

    C. Arthur and Table time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. ViewPager 入门一

    使用ViewPager能够得到不同view的切换效果 例如以下图,实现了四个view间的相互滑动 一.新建项目,引入ViewPager控件 ViewPager.它是google SDk中自带的一个附加 ...

  7. Poj1734题解

    题目大意 求一个无向图的最小环 题解 假设是有向图的话.仅仅须要令f[i][i]=+∞,再floyd就可以: 对无向图.应该在floyd算法循环至k的一開始进行例如以下操作: 枚举i和j,假设点i存在 ...

  8. android图片特效处理之光照效果

    这篇将讲到图片特效处理的光照效果.跟前面一样是对像素点进行处理,算法是通用的. 算法原理:图片上面的像素点按照给定圆心,按照圆半径的变化,像素点的RGB值分别加上相应的值作为当前点的RGB值. 例: ...

  9. vue.js最最最最简单实例

    vue.js最最最最简单实例 一.总结 一句话总结: 1.vue.js实现实现数据的双向绑定用的是什么标记? 双大括号:比如{{message}} 2.vue数据循环输出的标记是什么? 用的是标签的v ...

  10. python学习 第六天课后总结:

    <br class="Apple-interchange-newline"><div></div>       python学习 第六天课后总结 ...