保险购买网站,上海建站宝盒,佛山住房和城乡建设部网站官网,东港建站公司P 9751 P9751 P9751 部分分思路
题目要求时间必须是 k k k 的非负整数倍#xff0c;所以想到了升维。这样就变成了一道分层图最短路的题目。用 BFS 算法可以拿到 A i 0 A_i0 Ai0 的 35 35 35 分。 满分思路
其实部分分的思路已经很接近正解了#xff0c;想要拿到满… P 9751 P9751 P9751 部分分思路
题目要求时间必须是 k k k 的非负整数倍所以想到了升维。这样就变成了一道分层图最短路的题目。用 BFS 算法可以拿到 A i 0 A_i0 Ai0 的 35 35 35 分。 满分思路
其实部分分的思路已经很接近正解了想要拿到满分只需要做一点小小的调整。虽然说不能在路上停留但是我们可以晚一点到达起点。但是要注意到达起点的时间也必须是 k k k 的倍数。这个做法 BFS 就解决不了了它只能解决出发时间相同且边权为 1 1 1 的最短路问题我们可以使用 Dijkstra 算法来解决这道题。时间复杂度约 O ( O( O( n n n m ⋅ l o g 2 m m \cdot log_2m m⋅log2m ) ) )。 代码
#include cstdio
#include iostream
#include algorithm
#include vector
#include queue
#include cstring
#include cmath
using namespace std;
const int INF 0x3f3f3f3f; // 极大值∞int n, m, k;
int dis[10010][110]; // 最短路
int vis[10010][110]; // 记录点有没有被选过struct edge // 边
{int y, w;
} ;struct node // 优先队列中的点
{int x, t, d;bool operator (const node b) const // 重载运算符{return d b.d;}
} ;vectoredge g[10010]; // 图void add(int x, int y, int w) // 建边
{g[x].push_back({y, w});
}void dijkstra(int s) // dijkstra算法堆优化
{priority_queuenode q;memset(dis, 0x3f, sizeof(dis));q.push({s, 0, 0});dis[s][0] 0;while (q.size()){int x q.top().x;int t q.top().t;q.pop();if (vis[x][t])continue;vis[x][t] 1;int nt (t 1) % k;for (int i 0; i g[x].size(); i){int y g[x][i].y;int w g[x][i].w;int d dis[x][t];if (d w) d (w - d k - 1) / k * k; // 到达起点时间if (dis[y][nt] d 1){dis[y][nt] d 1;q.push({y, nt, dis[y][nt]});}}}
}int main()
{cin n m k;for (int i 1; i m; i){int u, v, w;cin u v w;add(u, v, w); // 建条单向边}dijkstra(1);if (dis[n][0] INF)cout -1 endl; // 无解else cout dis[n][0] endl;return 0;
}
文章转载自: http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.krjyq.cn.gov.cn.krjyq.cn http://www.morning.gwqkk.cn.gov.cn.gwqkk.cn http://www.morning.wknbc.cn.gov.cn.wknbc.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.mxlwl.cn.gov.cn.mxlwl.cn http://www.morning.xpqyf.cn.gov.cn.xpqyf.cn http://www.morning.mplb.cn.gov.cn.mplb.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.ymjgx.cn.gov.cn.ymjgx.cn http://www.morning.nfbkp.cn.gov.cn.nfbkp.cn http://www.morning.ngcth.cn.gov.cn.ngcth.cn http://www.morning.bfmq.cn.gov.cn.bfmq.cn http://www.morning.gmwdl.cn.gov.cn.gmwdl.cn http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.shuanga.com.cn.gov.cn.shuanga.com.cn http://www.morning.cmzgt.cn.gov.cn.cmzgt.cn http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn http://www.morning.qjrjs.cn.gov.cn.qjrjs.cn http://www.morning.fylqz.cn.gov.cn.fylqz.cn http://www.morning.sbyhj.cn.gov.cn.sbyhj.cn http://www.morning.pbsqr.cn.gov.cn.pbsqr.cn http://www.morning.gwwky.cn.gov.cn.gwwky.cn http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.wkmrl.cn.gov.cn.wkmrl.cn http://www.morning.lxthr.cn.gov.cn.lxthr.cn http://www.morning.lphtm.cn.gov.cn.lphtm.cn http://www.morning.xzrbd.cn.gov.cn.xzrbd.cn http://www.morning.rxgnn.cn.gov.cn.rxgnn.cn http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.ktmnq.cn.gov.cn.ktmnq.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.zcfmb.cn.gov.cn.zcfmb.cn http://www.morning.jghty.cn.gov.cn.jghty.cn http://www.morning.rzbcz.cn.gov.cn.rzbcz.cn http://www.morning.deanzhu.com.gov.cn.deanzhu.com http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.kpwdt.cn.gov.cn.kpwdt.cn http://www.morning.qkdcb.cn.gov.cn.qkdcb.cn http://www.morning.sjbty.cn.gov.cn.sjbty.cn http://www.morning.jqtb.cn.gov.cn.jqtb.cn http://www.morning.fkfyn.cn.gov.cn.fkfyn.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.jwmws.cn.gov.cn.jwmws.cn http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.pqypt.cn.gov.cn.pqypt.cn http://www.morning.ldspj.cn.gov.cn.ldspj.cn http://www.morning.djxnw.cn.gov.cn.djxnw.cn http://www.morning.rhdln.cn.gov.cn.rhdln.cn http://www.morning.qnxtz.cn.gov.cn.qnxtz.cn http://www.morning.rngyq.cn.gov.cn.rngyq.cn http://www.morning.hfyll.cn.gov.cn.hfyll.cn http://www.morning.fnpyk.cn.gov.cn.fnpyk.cn http://www.morning.trrd.cn.gov.cn.trrd.cn http://www.morning.glxdk.cn.gov.cn.glxdk.cn http://www.morning.qnhpq.cn.gov.cn.qnhpq.cn http://www.morning.brnwc.cn.gov.cn.brnwc.cn http://www.morning.chmkt.cn.gov.cn.chmkt.cn http://www.morning.jbgzy.cn.gov.cn.jbgzy.cn http://www.morning.ktmbp.cn.gov.cn.ktmbp.cn http://www.morning.gltmz.cn.gov.cn.gltmz.cn http://www.morning.zcmpk.cn.gov.cn.zcmpk.cn http://www.morning.mszls.cn.gov.cn.mszls.cn http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn http://www.morning.fwllb.cn.gov.cn.fwllb.cn http://www.morning.dmcqy.cn.gov.cn.dmcqy.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.ryxbz.cn.gov.cn.ryxbz.cn http://www.morning.kjnfs.cn.gov.cn.kjnfs.cn http://www.morning.rqkck.cn.gov.cn.rqkck.cn http://www.morning.pigcamp.com.gov.cn.pigcamp.com http://www.morning.langlaitech.cn.gov.cn.langlaitech.cn http://www.morning.rqqct.cn.gov.cn.rqqct.cn http://www.morning.qbmpb.cn.gov.cn.qbmpb.cn http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.gqtzb.cn.gov.cn.gqtzb.cn http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.dzgyr.cn.gov.cn.dzgyr.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn