题目链接:http://poj.org/problem?id=1511

就是求从起点到其他点的最短距离加上其他点到起点的最短距离的和 , 注意路是单向的.

因为点和边很多, 所以用dijkstra优先队列的做法.

起点到其他点的最短距离之和就是dij一下 . 要求其他点到起点的最短距离的话就是把原先边的方向反向一下,然后再求起点到其他点的最短距离之和 , 同样dij一下.

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue> using namespace std;
const int MAXN = 1e6 + ;
typedef long long LL;
typedef pair <LL , LL> P;
vector <P> edge[MAXN]; //这里邻接表二元组(pair)的second代表边的终点标号, first代表边的距离
bool ok[MAXN];
LL d[MAXN] , INF = 1e12;
int a[MAXN] , b[MAXN] , c[MAXN]; //分别代表边的起点 终点 权值 void init(int n) {
for(int i = ; i <= n ; i++) {
d[i] = INF;
ok[i] = false;
edge[i].clear();
}
} void dijkstra(int s) {
d[s] = ;
priority_queue <P , vector<P> , greater<P> > Q; //优先队列 二元组的first:最短距离 second:点的标号
Q.push(P( , s)); //压入起点
while(!Q.empty()) {
P p = Q.top();
Q.pop();
int v = p.second;
if(ok[v]) {
continue;
}
ok[v] = true;
for(int i = ; i < edge[v].size() ; i++) {
P temp = edge[v][i];
if(d[temp.second] > d[v] + temp.first) {
d[temp.second] = d[v] + temp.first;
Q.push(P(d[temp.second] , temp.second));
}
}
}
} int main()
{
int t , n , m;
LL u , v , cost;
scanf("%d" , &t);
while(t--) {
scanf("%d %d" , &n , &m);
init(n);
for(int i = ; i < m ; i++) {
scanf("%d %d %d" , &a[i] , &b[i] , &c[i]);
edge[a[i]].push_back(P(LL(c[i]) , LL(b[i])));
}
LL res = ;
dijkstra();
for(int i = ; i <= n ; i++) {
res += d[i];
}
init(n);
for(int i = ; i < m ; i++) {
edge[b[i]].push_back(P(LL(c[i]) , LL(a[i]))); //每条边反向一下
}
dijkstra();
for(int i = ; i <= n ; i++) {
res += d[i];
}
printf("%lld\n" , res);
}
}

POJ 1511 - Invitation Cards (dijkstra优先队列)的更多相关文章

  1. POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))

    题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...

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

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

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

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

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

  5. [POJ] 1511 Invitation Cards

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

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

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

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

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

  8. Poj 1511 Invitation Cards(spfa)

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

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

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

随机推荐

  1. MySQL学习笔记二

    Ø function 函数 函数的作用比较大,一般多用在select查询语句和where条件语句之后.按照函数返回的结果, 可以分为:多行函数和单行函数:所谓的单行函数就是将每条数据进行独立的计算,然 ...

  2. .propertie文件注释

    在.properties文件中注释,前边加#就可以

  3. uva12169 Disgruntled Judge

    扩展欧几里得. 枚举a,根据x1,x3和递推式可得. (a+1)*b-k*mod=f[3]-a*a*b. 通过扩展欧几里得求出b. 带入原式进行计算. #include<cstdio> # ...

  4. SQL查看数据库所用用户表数量和使用的空间

    SQL Server数据库管理员通常硬盘空间奋斗,不断努力清理“表”,撰写许多查询,发现该表使用的硬盘空间. 本文介绍了如何查询系统表的空间使用情况,帮助数据库管理员识别正在使用最多的空间,以便存档旧 ...

  5. bzoj1412: [ZJOI2009]狼和羊的故事

    空地之间开始没有连然后一直WA...题意混乱...尴尬. #include<cstdio> #include<cstring> #include<iostream> ...

  6. eclipse启动出现“An Error has Occurred. See the log file”解决方法

    最近在启动eclipse时出现了“An Error has Occurred. See the log file”的错误,点击确定后也不能启动eclipse.查看log文件,出现类似: java.la ...

  7. Jquery 模板插件 jquery.tmpl.js 的使用方法(2):嵌套each循环,temp调用(使用预编译的模板缓存)

    直接上代码吧 一:主窗口 /*#region SendChooseTargetTemplate 发送候选人主窗口模板*/ var SendChooseTargetTemplate = ''; Send ...

  8. cocos2d_x 问题汇总

    1.生成so文件时,报“No rule to make target ”错误 解决方法:将.\xxx[appname]\proj.android\obj\local\armeabi\objs中的文件全 ...

  9. 05day2

    05day1 没什么可说,一道模拟水题,两道裸的模板题.05day2 是几天以来最难的一次.   圆排列 动态规划 [问题描述] 有 N 个人顺时针围在一圆桌上开会,他们对身高很敏感. 因此决定想使得 ...

  10. BMP图像格式

    BMP(全称Bitmap)是Window操作系统中的标准图像文件格式,可以分成两类:设备相关位图(DDB)和设备无关位图(DIB),使用非常广.它采用位映射存储格式,除了图像深度可选以外,不采用其他任 ...