poj 1511-- Invitation Cards (dijkstra+优先队列)
刚开始想复杂了,一直做不出来,,,其实就是两遍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+优先队列)的更多相关文章
- POJ 1511 - Invitation Cards (dijkstra优先队列)
题目链接:http://poj.org/problem?id=1511 就是求从起点到其他点的最短距离加上其他点到起点的最短距离的和 , 注意路是单向的. 因为点和边很多, 所以用dijkstra优先 ...
- POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))
题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...
- 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 ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
- 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 ...
随机推荐
- Zap简单使用
前言 zap 是 uber 开源的一个日志记录的包, uber 在 go 的领域建树颇多, zap 更是优秀, 相比于自带的 log ,他有更多的功能, 当然, 最显眼的还是他很快, 本文介绍 zap ...
- windows打包脚本出现 /bin/sh^M: 坏的解释器: 没有那个文件或目录 错误
1.错误描述 我在Windows 10 系统下打包dolphinscheduler,上传到centos7解压之后,执行脚本报如下错误: -bash: ./dolphinscheduler-daemon ...
- python常见题型
语言特性 1. 谈谈对 Python 和其他语言的区别 2. 简述解释型和编译型编程语言 3. Python 的解释器种类以及相关特点? 4. Python3 和 Python2 的区别? 5. Py ...
- 【IMP】导出的时候显示ddl建表语句
导出数据后,在导入的时候想要显示出建表语句,可以用show=y这个选项来实现 imp test/test file=test.dmp ignore=y show=y fromuser=test1 to ...
- 解决ROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'creat table study_record( id int(11) not null
之前一直用的好好的,突然就出现了这个错误: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual tha ...
- CS_WHERE_USED_MAT 反查BOM的成品CS15
可能很多人都用过BOM展开的函数,但是有的时候,需要通过组件去反查BOM的成品,而这时候就需要用到函数 CS_WHERE_USED_MAT来实现,而对于CS_WHERE_USED_MAT只能反查到上一 ...
- 【Android】编译报错 Annotation processors must be explicitly declared now 解决方案
问题 在网上下载一个demo,因为版本久远,里面添加了本地 Butter Knife 的jar包,在编译时报错 Annotation processors must be explicitly dec ...
- Ubuntu源、Python虚拟环境及pip源配置
Ubuntu 命令行更改源 在修改source.list前,最好先备份一份 软件源的地址配置文件在 /etc/apt/sources.list 执行备份命令 sudo cp /etc/apt/sour ...
- windows桌面快速添加控制面板网络等图标
默认安装后的windows系统只有回收站. rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0
- JAVA高并发集合详解
Queue(队列)主要是为了高并发准备的容器Deque:双端队列,可以反方向装或者取 最开始jdk1.0只有Vector和hashtable 默认所有方法都实现了synchronized锁,线程安全但 ...