题目链接: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. Codeforces Beta Round #9 (Div. 2 Only)D

    短小精悍的代码 dp[i][j] +=dp[k][j-1]*[i-k-1][j-1] i个结点 J层 #include <iostream> #include<cstdio> ...

  2. add-two-numbers-ii

    注意:有一种好的方法,是将链表倒转,然后依次相加. 但是,按照题目要求,用了不改变原链表的方法. 就是将两个链表增加到相同长度,然后递归相加,子函数返回后处理进位. https://leetcode. ...

  3. LA 2797 (平面直线图PLSG) Monster Trap

    题意: 平面上有n条线段,一次给出这n条线段的两个端点的坐标.问怪兽能否从坐标原点逃到无穷远处.(两直线最多有一个交点,且没有三线共交点的情况) 分析: 首先说明一下线段的规范相交:就是交点唯一而且在 ...

  4. Asp.Net MVC Views页面不包含“GetEnumerator”的公共定义

    “/”应用程序中的服务器错误. 编译错误 说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码. 编译器错误消息: CS1579: “Web.Model ...

  5. Hadoop configration类分析

    configration这个类是分析hadoop源代码一个很好地入口. 先从需求说起.对于一个大型的文件系统,基于配置文件可以增强灵活性.congfigration类就是为了管理配置文件的. 配置文件 ...

  6. 对于GLM的理解,与方差分析的对比

    最近遇到一个问题,如果因变量为一个连续变量(如胰岛素水平),主要考察的变量为分组变量(如正常血糖组,前糖尿病组,糖尿病组三组),现在的目的是想看调整多种变量(包括多个连续性变量和分类变量)后,胰岛素水 ...

  7. 最简单的视音频播放示例8:DirectSound播放PCM

    本文记录DirectSound播放音频的技术.DirectSound是Windows下最常见的音频播放技术.目前大部分的音频播放应用都是通过DirectSound来播放的.本文记录一个使用Direct ...

  8. mysql5.6子查询的优化

    https://dev.mysql.com/doc/refman/5.6/en/subquery-optimization.html Semi-join in MySQL 5.6   MySQL 5. ...

  9. hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)

    这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...

  10. 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree

    // 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...