Gym - 101628F Find the Inn dijkstra,读边时计算新权值

时间:2021-03-22 20:28:31

题意: 给n个点m条边及每条边所花费的时间,经过给定的p个点时会停留k秒,要求在t秒内从1号点走到n号点,若可以走到输出最短时间,若不行输出-1.。

题解:读取边时,将每个点停留的时间加到以其为终点的边的花费上。比如边1 2 10,且2是给定的停留点(设停留5s),则读入这条边时按 1 2 15 读入,然后正常dijkstra即可。

坑点:单位有分有秒/有向边/参数貌似很多。由于一直找不到wa在哪里,先是怀疑爆int,然后把那个多余的map简化成了bool数组,又把输出时的if,else优化了一下,(只是把代码弄得短一些,然并卵)最后怀疑邻接链表有问题,全部改成链式前向星ORZ,依然并卵。又怀疑dijkstra要加vis数组,依然并卵。最后改priority_queue,先发现prioirity_queue<int,vector<int>,greater<int> > 报错,只能用了一个point结构体重载+-,然而并没有什么卵用!!最后发现自己多写了一行T*=60 Orz

ac代码:

#include<iostream>
#include<vector>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
const ll maxn = 1e5 + ;
ll n, m, k, p;
long long T;
vector< pair<ll, ll> > E[maxn];
ll d[maxn];
map<ll, ll> pine;
void init() {
for (ll i = ; i <maxn; i++) E[i].clear(), d[i] = 2e18;
}
int main()
{ while (cin >> n >> m >> T >> k >> p) {
//T *= 60;
for (ll i = ; i<p; i++) {
ll x; cin >> x; pine[x]++;
} init();
for (ll i = ; i < m; i++) {
ll x, y;
long long z;
cin >> x >> y >> z;
z *= ; if (pine.count(y))
E[x].push_back(make_pair(y, z + k));
else E[x].push_back(make_pair(y, z));
} ll s = , t = n;
priority_queue<pair<ll, ll> > Q;
d[s] = ; Q.push(make_pair(-d[s], s));
while (!Q.empty()) {
ll now = Q.top().second;
Q.pop(); for (ll i = ; i < E[now].size(); i++)
{
ll v = E[now][i].first;
if (d[v] > d[now] + E[now][i].second) {
d[v] = d[now] + E[now][i].second; Q.push(make_pair(-d[v], v));
}
}
} if (d[t] == 2e18 || d[t] > T * )cout << - << endl;
else cout << d[t] << endl; }
}

一度改得面目全非:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const long long maxn = ;
typedef long long ll;
ll n, m, k, p,T;
ll pine[maxn];
ll head[maxn], now,dis[maxn],vis[maxn];
struct edge {
ll to, next, val;
}e[*maxn];
void addedge(ll x, ll y, ll z) {
e[++now].to = y, e[now].val = z, e[now].next = head[x], head[x] = now;
}
struct point {
ll val, id;
point(ll id, ll val) :id(id), val(val) {}
bool operator <(const point &x) const {
return val > x.val;
}
};
void dij(ll s) {
priority_queue<point>Q;
Q.push(point(s, ));
vis[s] = ;
dis[s] = ;
while (!Q.empty()) {
ll cur = Q.top().id;
Q.pop();
vis[cur] = ;
for (ll i = head[cur]; i != -; i = e[i].next) {
ll id = e[i].to;
if (!vis[id] && (dis[cur] + e[i].val < dis[id] || dis[id] == -)) {
dis[id] = dis[cur] + e[i].val;
Q.push(point(id, dis[id]));
}
}
} }
int main(){ scanf("%lld%lld%lld%lld%lld", &n, &m, &T, &k, &p);
memset(head, -, sizeof(head));
memset(dis, -, sizeof(dis));
//T *= 60;
for (ll i = ; i<p; i++) {
ll x; scanf("%lld", &x); pine[x] = ;
}
for (ll i = ; i < m; i++) {
ll x, y;
long long z;
scanf("%lld%lld%lld", &x, &y, &z);
z *= ;
addedge(x, y, k*pine[y] + z);
}
dij();
if (dis[n] <= T * ) printf("%lld", dis[n]);
else printf("-1");
}