山东省第七届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 ...
随机推荐
- air 桌面应用发布后可以删除的文件
****\Adobe AIR\Versions\1.0 下的文件夹Resources,可以整个删除 ***META-INF\AIR目录下的application.xml配置文件可修改initialWi ...
- 4 python内置函数
1.内置函数的整体介绍 内置参数官方详解 https://docs.python.org/3/library/functions.html?highlight=built#ascii 2.各内置函数介 ...
- 将JDBC的resultSet映射到JavaBaen
// 执行赋值后SQL, rs=pstm.executeQuery(); //判断是否有返回结果,有下一行rs.next()方法为true ...
- WAL 【转】
重做日志:每当有操作执行前,将数据真正更改时,先前相关操作写入重做日志.这样当断电,或者一些意外,导致后续任务无法完成时,系统恢复后,可以继续完成这些更改 撤消日志:当一些更改在执行一半时,发生意外, ...
- The maximum column size is 767 bytes (Mysql)
ERROR app.wsutils 419 INCRON: Error: ('HY000', '[HY000] [MySQL][ODBC 5.2(w) Driver][mysqld-5.7.7-rc ...
- SpringCloud之Eureka 服务注册和服务发现基础篇2
上篇文章讲解了SpringCloud组件和概念介绍,接下来讲解一下SpringCloud组件相关组件使用.原理和每个组件的作用的,它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix ...
- poj2456(二分+贪心)
题目链接:http://poj.org/problem?id=2456 题意: 有n个呈线性排列的牲畜堋,给出其坐标,有c头牛,求把两头牛的最短距离的最大值. 思路: 先将坐标排个序.两头牛的最短距离 ...
- Python3 range() 函数用法
Python3 range() 函数用法 Python3 内置函数 Python3 range() 函数返回的是一个可迭代对象(类型是对象),而不是列表类型, 所以打印的时候不会打印列表. Pyth ...
- 97. Interleaving String (String; DP)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- LSTM Accuracy
Training iter #1: Batch Loss = 1.234543, Accuracy = 0.29866665601730347PERFORMANCE ON TEST SET: Batc ...