Problem D. Dwarf Tower spfa
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的更多相关文章
- Codeforces Gym 100269D Dwarf Tower spfa
Dwarf Tower 题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a ...
- noip模拟赛 dwarf tower
[问题描述]Vasya在玩一个叫做"Dwarf Tower"的游戏,这个游戏中有n个不同的物品,它们的编号为1到n.现在Vasya想得到编号为1的物品.获得一个物品有两种方式:1. ...
- dwarf tower
dwarf tower(dwarf.cpp/c/pas)[问题描述]Vasya在玩一个叫做"Dwarf Tower"的游戏,这个游戏中有n个不同的物品,它们的编号为1到n.现在Va ...
- D.Dwarf Tower
Vasya在玩一个叫做"Dwarf Tower"的游戏,这个游戏中有n个不同的物品, 它们的编号为1到n.现在Vasya想得到编号为1的物品. 获得一个物品有两种方式: 直接购买该 ...
- Codeforces Gym 100269 Dwarf Tower (最短路)
题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game na ...
- hdoj--3666--THE MATRIX PROBLEM(差分约束+SPFA深搜)
THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HNU 12847 Dwarf Tower(最短路+队列优化)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12847 解题报告:有n样物品,编号从1到n第i样物品可以通过金 ...
- poj 1511(spfa)
---恢复内容开始--- http://poj.org/problem?id=1511 一个spfa类的模板水题. 题意:就是求从1到n个点的来回的所有距离和. 对spfa类的题还是不太熟练,感觉还是 ...
- BZOJ1233 [Usaco2009Open]干草堆tower 和 BZOJ3549 [ONTAK2010]Tower
题意 Problem 3549. -- [ONTAK2010]Tower 3549: [ONTAK2010]Tower Time Limit: 10 Sec Memory Limit: 64 MBS ...
随机推荐
- HDOJ(1069)最长下降子序列
每个箱子可有3种叠加方式,所以有3*n个箱子.将箱子按长度由大到小排序,有求箱子按宽度的最长下降子序列的高度之和即可. #include<cstdio> #include<algor ...
- jQuery做出手风琴效果
今天学到JQuery中的遍历-siblings,便手痒做了个手风琴的动态效果,有一点收获,分享给大家.mouseout的时候一定要记得opacity必须设置,不然li的opacity会保持mousem ...
- MySQL Sending data导致查询很慢的问题详细分析
这两天帮忙定位一个MySQL查询很慢的问题,定位过程综合各种方法.理论.工具,很有代表性,分享给大家作为新年礼物:) [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据的 ...
- 杂项:BI(商业智能)
ylbtech-杂项:BI(商业智能) 商业智能(BI,Business Intelligence). BI(Business Intelligence)即商务智能,它是一套完整的解决方案,用来将企业 ...
- TS学习之基础类型
1.布尔值 let isDone:boolean = false 2.数字(支持二,八,十,十六进制) let width:number = 20 3.字符串 let name:string = &q ...
- Python-RabbitMQ消息队列实现rpc
客户端通过发送命令来调用服务端的某些服务,服务端把结果再返回给客户端 这样使得RabbitMQ的消息发送端和接收端都能发送消息 返回结果的时候需要指定另一个队列 服务器端 # -*- coding:u ...
- C#使用Command将dataGrideView表格内数据与数据库交互
本文主要介绍通过Command类使用SQL插入指令insert与查询指令select将dataGrideView表格内添加至数据库,与从数据库读出数据存放在dataGrideView表格中. C#制作 ...
- asp中实现lable自动换行
asp中实现lable自动换行 因为在用Label标签显示内容时,内容太多,想实现自动换行.我们知道在WINFORM中程序中,有一个属性是AUTOSIZE 改成FALSE 是可以实现的.但是在ASP. ...
- Spring 3.x 企业引用开发实战(陈雄华/林开雄)
目录 ... 第一章:Spring概述 IoC:BeanFactory.Context.El(SpringEL表达式) AOP:允许JVM虚拟机启动时使用代理类在运行时期修改指定类的字节码,改变一个类 ...
- Umbraco项目发布错误 --More than one type want to be a model for content type authorize
在开发项目时,解决方案下面包括三个项目 MyUmbracoProject MyUmbracoProject.Core MyUmbracoProject.FrontEnd 第一个项目MyUmbracoP ...