山东省第七届ACM竞赛 C题 Proxy (Dijkstra算法,单源路径最短问题)
题意:给定0-n+1个点,和m条边,让你找到一条从0到n+1的最短路,输出与0相连的结点。。。
析:很明显么,是Dijkstra算法,不过特殊的是要输出与0相连的边,所以我们倒着搜,也是从n+1找到0,
那么不就能找到与0相连的边么,注意判断相等值的时候。当时写错了好多次,就是没有考虑好边界。
代码如下:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring> using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1000 + 100;
int n;
struct edge{
int from, to, dist;
edge(int u, int v, int d) : from(u), to(v), dist(d) { }
};
struct Headnode{
int d, u;
Headnode(int dd, int uu) : d(dd), u(uu) { }
bool operator < (const Headnode &rhs) const {
return d > rhs.d;
}
};
struct Dijkstra{
int m;
vector<edge> edges;
vector<int> G[maxn];
bool done[maxn];
int d[maxn];
int p[maxn]; void init(){
for(int i = 0; i <= n+1; ++i) G[i].clear();
edges.clear();
} void addedge(int f, int t, int d){
edges.push_back(edge(f, t, d));
m = edges.size();
G[f].push_back(m-1);
} void dijkstra(int s){
priority_queue<Headnode> q;
for(int i = 0; i <= n+1; ++i) d[i] = INF;
d[s] = 0; memset(done, 0, sizeof(done));
q.push(Headnode(0, s));
while(!q.empty()){
Headnode x = q.top(); q.pop();
int u = x.u;
if(done[u]) continue;
done[u] = true;
for(int i = 0; i < G[u].size(); ++i){
edge &e = edges[G[u][i]];
if(d[e.to] >= d[u] + e.dist){
d[e.to] = d[u] + e.dist;
p[e.to] = u;
q.push(Headnode(d[e.to], e.to));
}
else if(d[e.to] == d[u] + e.dist) p[e.to] = min(u, p[e.to]);
}
}
}
};
Dijkstra dijk; int main(){
int T, m; cin >> T;
while(T--){
dijk.init();
scanf("%d %d", &n, &m);
int u, v, c;
for(int i = 0; i < m; ++i){
scanf("%d %d %d", &u, &v, &c);
dijk.addedge(v, u, c);
}
dijk.dijkstra(n+1); if(dijk.d[0] >= INF) printf("-1\n");
else if(dijk.p[0] == n+1) printf("0\n");
else printf("%d\n", dijk.p[0]);
}
return 0;
}
山东省第七届ACM竞赛 C题 Proxy (Dijkstra算法,单源路径最短问题)的更多相关文章
- 山东省第七届ACM竞赛 J题 Execution of Paladin (题意啊)
题意:鱼人是炉石里的一支强大种族,在探险者协会里,圣骑士有了一张新牌,叫亡者归来,效果是召唤本轮游戏中7个已死鱼人.如果死掉的不足7个,那么召唤的数量就会不足7. 鱼人有很多,下面的4个是: 寒光智者 ...
- 山东省第七届ACM省赛------Memory Leak
Memory Leak Time Limit: 2000MS Memory limit: 131072K 题目描述 Memory Leak is a well-known kind of bug in ...
- 山东省第七届ACM省赛------Reversed Words
Reversed Words Time Limit: 2000MS Memory limit: 131072K 题目描述 Some aliens are learning English. They ...
- 山东省第七届ACM省赛------Triple Nim
Triple Nim Time Limit: 2000MS Memory limit: 65536K 题目描述 Alice and Bob are always playing all kinds o ...
- 山东省第七届ACM省赛------The Binding of Isaac
The Binding of Isaac Time Limit: 2000MS Memory limit: 65536K 题目描述 Ok, now I will introduce this game ...
- 山东省第七届ACM省赛------Fibonacci
Fibonacci Time Limit: 2000MS Memory limit: 131072K 题目描述 Fibonacci numbers are well-known as follow: ...
- 山东省第七届ACM省赛------Julyed
Julyed Time Limit: 2000MS Memory limit: 65536K 题目描述 Julyed is preparing for her CET-6. She has N wor ...
- 山东省第七届省赛 D题:Swiss-system tournament(归并排序)
Description A Swiss-system tournament is a tournament which uses a non-elimination format. The first ...
- 山东省第七届ACM省赛
ID Title Hint A Julyed 无 B Fibonacci 打表 C Proxy 最短路径 D Swiss-system tournament 归并排序 E The Binding of ...
随机推荐
- Json字符串转Dictionary
/// <summary> /// json字符串转Dictionary /// </summary> /// <param name="json"& ...
- mydumper使用
一.下载安装,打开https://launchpad.net/mydumper #wget https://launchpadlibrarian.net/185032423/mydumper-0.6. ...
- unity VideoPlayer
Events(事件) started:在调用play()后立刻调用 prepareCompleted:播放器准备完成时 seekCompleted:缓冲完成时
- 迷你MVVM框架 avalonjs 学习教程15、属性监听与模块通信
avalon的ViewModel对象从其内部EventManager里继承了三个方法,$watch.$unwatch.$fire三个方法,它们就是我们本节的主题. 词如其名,非常直白,一看就知道做什么 ...
- servlet第三篇
Servlet是一个供其他Java程序(Servlet引擎)调用的Java类,它不能独立运行,它的运行完全由Servlet引擎来控制和调度. 针对客户端的多次Servlet请求,通常情况下,服务器只会 ...
- Spark数据类型SparseMatrix 解释
http://blog.csdn.net/sinat_29508201/article/details/54089771 parseMatrix Spark的mllib包中提供了机器学习的两种基本数据 ...
- 1、JDK自带注解
1.何为注解? 概念:注解(Annotation),也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举是在同一个层次.它可以声明在包.类.字段.方法.局部变量. ...
- MySql LeftJoin On 与 Where的差异
[MySql LeftJoin On 与 Where的差异] 存在两张表: 分别插入数据: 下面的语句一与语句二会产生不同的结果: 语句一: 结果: 语句二: 结果: 为什么会存在差异,这和on与wh ...
- 第五章 二叉树(a)树
- Maven项目标准目录结构
-----------------------siwuxie095 Maven 项目标准目录结构 1.Maven 项目分为两种 (1)Java 项目 (2)Web 项目 2.对于 Java 项目 其中 ...