http://codeforces.com/gym/100269/attachments

首先建图,然后图中每条边的权值是会变化的,是由dis[x] + dis[y]  --->   dis[make],然后就相当于新增加一个原点0,求0到1的最短距离

如果用了2更新4失败,但是2本来不是最优的,就是可以用7和8使得更优,那这样会不会漏掉最优解?答案是不会的,因为使用到7和8能更新2得时候,就会把2重新丢尽队列

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 2e5 + ;
struct Edge {
int u, v, w, tonext;
}e[maxn * ];
int first[maxn], num;
void addEdge(int u, int v, int w) {
++num;
e[num].u = u, e[num].v = v, e[num].w = w, e[num].tonext = first[u];
first[u] = num;
}
LL dis[maxn];
int in[maxn], tim[maxn];
bool spfa(int bx, int n) { //从bx开始,有n个顶点
queue<int> que;
while (!que.empty()) que.pop();
for (int i = ; i <= n; ++i) {
que.push(i);
in[i] = true;
tim[i]++;
}
while (!que.empty()) {
int u = que.front();
if (tim[u] > n) return true; //入队次数超过n次,出现负环
que.pop(); //in[u] = false ?
for (int i = first[u]; i; i = e[i].tonext) {
if (dis[e[i].v] > dis[e[i].u] + dis[e[i].w]) {
dis[e[i].v] = dis[e[i].u] + dis[e[i].w];
if (!in[e[i].v]) { //不在队列
que.push(e[i].v);
in[e[i].v] = true;
tim[e[i].v]++;
}
}
}
in[u] = false;
}
return false;
} void work() {
int n, m;
cin >> n >> m;
for (int i = ; i <= n; ++i) {
cin >> dis[i];
}
for (int i = ; i <= m; ++i) {
int f, x, y;
cin >> f >> x >> y;
addEdge(x, f, y);
addEdge(y, f, x);
}
spfa(, n);
cout << dis[] << endl; } int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
freopen("dwarf.in", "r", stdin);
freopen("dwarf.out", "w", stdout);
IOS;
work();
return ;
}

也可以贪心。

每次都取一个权值最小的出来,因为那个已经不可能更小了,直接删除,然后更新其他。

用set维护。dp

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
struct Node {
LL cost, id;
bool operator < (const struct Node & rhs) const {
if (cost != rhs.cost) return cost < rhs.cost;
else return id < rhs.id;
}
Node(LL _cost, LL _id) {
cost = _cost, id = _id;
}
};
set<Node>ss;
const int maxn = 1e6 + ;
LL dp[maxn];
vector<pair<int, int> > vc[maxn];
int getid[maxn];
void work() {
int n, m;
cin >> n >> m;
for (int i = ; i <= n; ++i) {
int val;
cin >> val;
dp[i] = val;
ss.insert(Node(val, i));
}
for (int i = ; i <= m; ++i) {
int a, b, c;
cin >> a >> b >> c;
vc[c].push_back(make_pair(b, a));
vc[b].push_back(make_pair(c, a));
}
set<Node> :: iterator it;
while (!ss.empty()) {
it = ss.begin();
LL id = it->id;
LL cost = it->cost;
ss.erase(it);
for (int i = ; i < vc[id].size(); ++i) {
int an = vc[id][i].first, to = vc[id][i].second;
if (!ss.count(Node(dp[to], to))) {
continue;
}
ss.erase(Node(dp[to], to));
dp[to] = min(dp[to], cost + dp[an]);
ss.insert(Node(dp[to], to));
}
}
printf("%lld\n", dp[]);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#else
freopen("dwarf.in","r",stdin);
freopen("dwarf.out","w",stdout);
#endif
work();
return ;
}

Problem D. Dwarf Tower spfa的更多相关文章

  1. Codeforces Gym 100269D Dwarf Tower spfa

    Dwarf Tower 题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a ...

  2. noip模拟赛 dwarf tower

    [问题描述]Vasya在玩一个叫做"Dwarf Tower"的游戏,这个游戏中有n个不同的物品,它们的编号为1到n.现在Vasya想得到编号为1的物品.获得一个物品有两种方式:1. ...

  3. dwarf tower

    dwarf tower(dwarf.cpp/c/pas)[问题描述]Vasya在玩一个叫做"Dwarf Tower"的游戏,这个游戏中有n个不同的物品,它们的编号为1到n.现在Va ...

  4. D.Dwarf Tower

    Vasya在玩一个叫做"Dwarf Tower"的游戏,这个游戏中有n个不同的物品, 它们的编号为1到n.现在Vasya想得到编号为1的物品. 获得一个物品有两种方式: 直接购买该 ...

  5. Codeforces Gym 100269 Dwarf Tower (最短路)

    题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game na ...

  6. hdoj--3666--THE MATRIX PROBLEM(差分约束+SPFA深搜)

    THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. HNU 12847 Dwarf Tower(最短路+队列优化)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12847 解题报告:有n样物品,编号从1到n第i样物品可以通过金 ...

  8. poj 1511(spfa)

    ---恢复内容开始--- http://poj.org/problem?id=1511 一个spfa类的模板水题. 题意:就是求从1到n个点的来回的所有距离和. 对spfa类的题还是不太熟练,感觉还是 ...

  9. BZOJ1233 [Usaco2009Open]干草堆tower 和 BZOJ3549 [ONTAK2010]Tower

    题意 Problem 3549. -- [ONTAK2010]Tower 3549: [ONTAK2010]Tower Time Limit: 10 Sec  Memory Limit: 64 MBS ...

随机推荐

  1. BZOJ1033:[ZJOI2008]杀蚂蚁

    我对模拟的理解:https://www.cnblogs.com/AKMer/p/9064018.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...

  2. Ubuntu14.04如何用root账号登陆系统

    在虚拟机VMWARE中安装完Ubuntu后,只能用新建的普通用户登陆,很不方便做实验:那如何用root用户登陆账号呢? (1)用普通账号登陆,打开终端terminal: (2)在terminal的输入 ...

  3. ss2

    一. *** 服务端配置 1. 在命令行窗口输入下面4行命令并回车执行 yum -y update yum install -y python-setuptools && easy_i ...

  4. JavaScript中对象的属性

    在JavaScript中,属性决定了一个对象的状态,本文详细的研究了它们是如何工作的. 属性类型 JavaScript中有三种不同类型的属性:命名数据属性(named data properties) ...

  5. http请求中的get和post的区别

    1.标准答案 GET在浏览器回退时是无害的,而POST会再次提交请求. GET产生的URL地址可以被Bookmark,而POST不可以. GET请求会被浏览器主动cache,而POST不会,除非手动设 ...

  6. stm32之外设控制

    本文将提到以下内容: 蜂鸣器 按键控制 电容触摸 温度传感器 红外 TFTLCD触摸屏 MPU6050传感器 SPI-FLASH SDIO_SD卡 ucos-III移植 一.蜂鸣器 蜂鸣器是一种一体化 ...

  7. HBase 官方文档

    HBase 官方文档 Copyright © 2010 Apache Software Foundation, 盛大游戏-数据仓库团队-颜开(译) Revision History Revision ...

  8. NodeJS”热部署“代码,实现动态调试(hotnode,可以实现热更新)

    NodeJS”热部署“代码,实现动态调试   开发中遇到的问题 如果你有 PHP 开发经验,会习惯在修改 PHP 脚本后直接刷新浏览器以观察结果,而你在开发 Node.js 实现的 HTTP 应用时会 ...

  9. [poj2135]Farm Tour(最小费用流)

    解题关键:最小费用流 代码一:bellma-ford $O(FVE)$  bellman-ford求最短路,并在最短路上增广,速度较慢 #include<cstdio> #include& ...

  10. 第六课 ROS的空间描述和变换

    1.空间描述与变换 有两个坐标系A和B,B坐标系中有一个点P,如何把B坐标系中的P映射到A坐标系呢,这就涉及到空间描述与变换, 先看一下旋转矩阵: 上面中间的行向量中的元素表示在B坐标系当中的元素用A ...