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 ...
随机推荐
- python -c 妙用
前言 python -c 命令还是有用的哈 正文 python的 -c 可以在命令行中调用 python 代码, 实际上 -c 就是 command 的意思 官方文档中解释为(节选自: python ...
- Nginx配置请求头
最近发现一个问题: IOS访问后台接口是,总是application/json;charset=utf-8 但是后台接口只支持大写的UTF-8,修改了Nginx的请求头之后正常. proxy_set_ ...
- Tomcat的整体架构
Tomcat通过连接器和容器这两个核心组件完成整体工作,连接器负责处理socket连接和网络字节流与Request和Response对象的转化:容器负责加载和管理Servlet,以及具体处理Reque ...
- PC个人隐私保护小方法
前言 近期爆出了腾讯读取用户浏览器浏览记录的消息.话不过说直接上图,懂的自然懂. 网上也有详细的分析文章,不管它读取后用来做什么,在你不知情的情况下读取了你的浏览器浏览记录,你说气不气. 虽然在整体大 ...
- Springboot之文件监控
背景:在实际环境部署构成中,由于特殊网络环境因素,有很多服务器之间的网络都是单向的,不能互相访问的,只有通过特定技术手段做到文件的单项摆渡,这就需要在两台服务器上分别写序列化程序和反序列化程序,这里不 ...
- jackson学习之四:WRAP_ROOT_VALUE(root对象)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- https://github.com/golang/go/wiki/CommonMistakes
CommonMistakes https://golang.org/doc/faq#closures_and_goroutines Why is there no goroutine ID? ¶ Go ...
- Defining Go Modules
research!rsc: Go & Versioning https://research.swtch.com/vgo shawn@a:~/gokit/tmp$ go get --helpu ...
- based on Greenlets (via Eventlet and Gevent) fork 孙子worker 比较 gevent不是异步 协程原理 占位符 placeholder (Future, Promise, Deferred) 循环引擎 greenlet 没有显式调度的微线程,换言之 协程
gevent GitHub - gevent/gevent: Coroutine-based concurrency library for Python https://github.com/gev ...
- ThinkPHP 5.0.24 反序列化RCE (Windows下EXP)
直接上exp吧,Windows下. <?php namespace think\process\pipes; class Windows { private $files = []; publi ...