[POJ 1511]Invitation Cards[链式前向星][SPFA]
来源:互联网 发布:qq采集软件 编辑:程序博客网 时间:2024/06/16 13:46
题目链接:[POJ 1511]Invitation Cards[链式前向星][SPFA]
题意分析:
每天从点1都会有
解题思路:
考虑本题边的个数和点的个数均
这里处理来回距离有一个小技巧,spfa可以求得点1到所有点的最短距离;那么把整个图转置一下(所有边都取它的反向边建图),再求1到所有点的最短距离,此时的最短距离就是所有点到1的最短距离。
为什么要用链式前向星存储图呢?因为本题是一个稀疏图,使用邻接矩阵极限样例情况下占用空间至少为
个人感受:
一直不能理解链式前向星有什么好的,结果这题就教做人了。死活就是T,改用链式前向星就A了。可怕 山口山
具体代码如下:
#include<cstdio>#include<iostream>#include<queue>#define ll long longusing namespace std;const ll INF = 0x7f7f7f7f;const int MAXN = 1e6 + 111;struct Edge{ int to, next; ll val;}edge[2][MAXN];ll dis[2][MAXN];int head[2][MAXN], cnt;bool in[MAXN];void make_map(int from, int to, ll v, int sta){ edge[sta][cnt].to = to; edge[sta][cnt].val = v; edge[sta][cnt].next = head[sta][from]; head[sta][from] = cnt;}void spfa(int s, int sta){ dis[sta][s] = 0; queue<int> q; q.push(s); in[s] = 1; while (q.size()) { int cur = q.front(); q.pop(); in[cur] = 0; for (int i = head[sta][cur]; ~i; i = edge[sta][i].next) { Edge &e = edge[sta][i]; if (dis[sta][e.to] > dis[sta][cur] + e.val) { dis[sta][e.to] = dis[sta][cur] + e.val; if (!in[e.to]) { in[e.to] = 1; q.push(e.to); } } } }}int main(){ int kase, p, q, u, v, w; scanf("%d", &kase); while (kase --) { scanf("%d%d", &p, &q); for (int i = 1; i <= p; ++i) { dis[0][i] = dis[1][i] = INF; head[0][i] = head[1][i] = -1; } cnt = 0; while (q --) { scanf("%d%d%d", &u, &v, &w); make_map(u, v, w, 0); make_map(v, u, w, 1); ++cnt; } spfa(1, 0); spfa(1, 1); ll ans = 0; for (int i = 2; i <= p; ++i) { // cout << dis[0][i] << ' ' << dis[1][i] << '\n'; ans += dis[0][i] + dis[1][i]; } printf("%lld\n", ans); } return 0;}
0 0
- POJ 1511 Invitation Cards 链式前向星+SPFA练习
- [POJ 1511]Invitation Cards[链式前向星][SPFA]
- HDU 1535 Invitation Cards 【SPFA + 链式前向星】
- poj1511 Invitation Cards(SPFA+前向星)
- poj 1511 Invitation Cards(优先队列优化Dijkstra+链式前向星存储边)
- HDU 1535 Invitation Cards (spfa, 链式前向星,逆向建图)
- POJ 1511 链式前向星+SPFA
- POJ 1511 Invitation Cards //spfa
- poj 1511 Invitation Cards--SPFA
- POJ 1511 Invitation Cards (Spfa)
- poj 1511 Invitation Cards(spfa)
- POJ 1511 Invitation Cards (SPFA)
- poj 1511 Invitation Cards(spfa)
- Poj 1511 Invitation Cards -- spfa
- POJ 1511 Invitation Cards(SPFA)
- POJ 1511 Invitation Cards(SPFA)
- Invitation Cards - POJ 1511 spfa
- Poj 1511 Invitation Cards(spfa)
- Swift基础篇——可选值
- Swift基础篇——枚举
- Swift基础篇——内存相关
- Swift基础篇——内外函数
- Swift基础篇——析构方法
- [POJ 1511]Invitation Cards[链式前向星][SPFA]
- java 虚拟机的生命周期
- Swift2.0——构造方法的使用
- Josephus问题解决方法一
- Swift中的打印日志的配置
- 关于大学考试的一点看法
- Swift 读取本地json文件时的异常捕获(try catch)的使用
- 垃圾收集 Garbage Collection
- PHP 页面抓取技术