刚开始想复杂了,一直做不出来,,,其实就是两遍dijkstra+优先队列(其实就是板子题,只要能有个好的板子,剩下的都不是事),做出来感觉好简单......

题意:有n个车站和n个志愿者,早上每个志愿者去一个站点,晚上回去,问最少的开销是多少。是一个有向图

先一遍dijkstra求出早上的开销,在把车站倒过来及1到4变成4到1,然后再一遍dijkstra求出晚上的开销。

不要用memset去初始化为INF, 本题如果用memset会wa,记得开long long;

 1 #include<cstdio>
2 #include<cstring>
3 #include<queue>
4 using namespace std;
5
6 const int INF = 1e9+10;
7 const int N = 1000010;
8 const int M = 1000010;
9 int tot;
10
11 struct Edge //dijkstra+优先队列板子
12 {
13 int v, cost, next;
14 }edge[M];
15
16 struct qnode
17 {
18 int v,c;
19 bool operator < (const qnode &x) const {
20 return c > x. c;
21 }
22 };
23
24 int head[M];
25 bool vis[N];
26 int dis[N];
27
28 void addedge(int u, int v, int w)
29 {
30 edge[tot]. v = v;
31 edge[tot]. cost = w;
32 edge[tot]. next = head[u];
33 head[u] = tot ++;
34 }
35
36 void dijkstra(int st)
37 {
38 memset(vis, 0, sizeof(vis));
39 for(int i=0;i<N;i++)
40 dis[i]=INF; //最好不要用memset去初始化为INF, 很容易wa
41 dis[st] = 0;
42 priority_queue <qnode> q;
43 q. push({st, 0});
44 while(! q. empty()){
45 qnode t = q. top();
46 q. pop();
47 if(vis[t. v])
48 continue;
49 vis[t. v] = true;
50 for(int i = head[t. v]; i != -1; i = edge[i]. next){
51 int v = edge[i]. v;
52 int cost = edge[i]. cost;
53 if(! vis[v] && dis[v] > dis[t. v] + cost){
54 dis[v] = dis[t. v] + cost;
55 q. push({v, dis[v]});
56
57 }
58 }
59 }
60 }
61
62 int a[N], b[N], c[N];
63
64 int main(){
65 int n, m,t;
66 scanf("%d",&t);
67 while(t--){
68 scanf("%d%d", &n, &m);
69 tot = 1;
70 memset(edge,0,sizeof edge);
71 memset(head, -1, sizeof(head));
72 for(int i=0;i<m;i++){
73 scanf("%d%d%d", &a[i], &b[i], &c[i]);
74 addedge(a[i], b[i], c[i]);
75 }
76 dijkstra(1); //早上的
77 int sum=0;
78 for(int i=2;i<=n;i++){
79 sum+=dis[i]; //把1到每个站的最小开销加起来
80 }
81 tot=1;
82 memset(edge,0,sizeof edge);
83 memset(head, -1, sizeof(head));
84 for(int i=0;i<m;i++){
85 addedge(b[i], a[i], c[i]); //正着是1到每个站,反过来就是每个站到1
86 }
87 dijkstra(1); //晚上的
88 for(int i=2;i<=n;i++){
89 sum+=dis[i]; //把每个站到1的最小开销加起来
90 }
91 printf("%d\n",sum);
92 }
93 return 0;
94 }

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

  1. POJ 1511 - Invitation Cards (dijkstra优先队列)

    题目链接:http://poj.org/problem?id=1511 就是求从起点到其他点的最短距离加上其他点到起点的最短距离的和 , 注意路是单向的. 因为点和边很多, 所以用dijkstra优先 ...

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

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

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

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

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

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

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

  6. [POJ] 1511 Invitation Cards

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

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

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

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

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

  9. Poj 1511 Invitation Cards(spfa)

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

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

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

随机推荐

  1. C#处理医学图像(二):基于Hessian矩阵的医学图像增强与窗宽窗位

    根据本系列教程文章上一篇说到,在完成C++和Opencv对Hessian矩阵滤波算法的实现和封装后, 再由C#调用C++ 的DLL,(参考:C#处理医学图像(一):基于Hessian矩阵的血管肺纹理骨 ...

  2. MySQL中的全局锁和表级锁

    全局锁和表锁 数据库锁设计的初衷是解决并发出现的一些问题.当出现并发访问的时候,数据库需要合理的控制资源的访问规则.而锁就是访问规则的重要数据结构. 根据锁的范围,分为全局锁.表级锁和行级锁三类. 全 ...

  3. ps的参数解释

    [root@bogon ~]# ps axuUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND user启动进程的用户 pid  表示进程标志 ...

  4. ORA-00245 control file backup operation failed 分析和解决

    一.问题说明 操作系统: RedHat 5.8 数据库: 11.2.0.3 2节点RAC. 使用RMAN 备份的时候,报如下错误: ORA-00245: control file backup fai ...

  5. C语言中左值和右值的区别(C语言学习笔记)

    重要的内容要重复强调: C语言的术语Ivalue指用于识别或定位一个存储位置的标识符.( 注意:左值同时还必须是可改变的) 其实rvalue的发明完全是为了搭配lvalue , rvalue你可以理解 ...

  6. SAPCAR使用说明

    1.首先看一下SAPCAR的功能usage:create a new archive:SAPCAR -c[vir][f archive] [-P] [-C directory]   [-A filen ...

  7. MySQL全面瓦解20:可编程性之流程控制语句

    背景 说到流程控制语句,我们在程序语法中用的比较多,比如C#的if..else...,while...,?: 等.同样的,在MySQL中,也有一些流程控制的语法,方便我们在写函数.存储过程的时候对逻辑 ...

  8. Python Debug工具

    最近在github上冒出了一个python的debug神器PySnooper,号称在debug时可以消灭print.那么该工具有哪些优点呢,如何使用该工具呢.本文就介绍该工具的优缺点和使用方式. 前言 ...

  9. Android N selectQualifiedNetwork分析

    前言: 参考:Android N wifi auto connect流程分析 后续 Android 8.0/9.0 wifi 自动连接评分机制 分析 前面说了,handleScanResults会去调 ...

  10. git branch --set-upstream-to=

    test@uat:/usr/server/app_server# git config --local -lcore.repositoryformatversion=0core.filemode=tr ...