pathTime Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1404 Accepted Submission(s): 298 Problem Description You have a directed weighted graph with n vertexes and m edges. The value of a path is the sum of the weight of the edges you passed. Note that you can pass any edge any times and every time you pass it you will gain the weight.
Input The input consists of multiple test cases, starting with an integer t (1≤t≤100), denoting the number of the test cases.
Output For each query, print one integer indicates the answer in line.
Sample Input 1 2 2 2 1 2 1 2 1 2 3 4
Sample Output 3 3 Hint 1->2 value :1 2->1 value: 2 1-> 2-> 1 value: 3 2-> 1-> 2 value: 3 |
假设现在某个路径位于v,来自u,是目前的第K大,那么可能是第K+1大的路径且由此状态延申的情况只有从此u点到从u出发长度最小的点的那个状态;以及从v点开始,刚好比到u这个路径要长的那个路径延申过来的状态这两种是可能成为K+1大的。这样进行搜索就能避免很多不必要的状态了。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 5e4 + 10;
struct fuck {
int v; ll w;
};
vector<fuck>G[N];
int n, m, q, qlimit;
bool cmp(fuck a, fuck b) {
return a.w < b.w;
}
int nq[N]; ll ans[N + 5000];
struct node {
int u, la, pos; ll d;
bool operator < (const node &a)const {
return d > a.d;
}
};
void solve() {
priority_queue<node>q;
for (int i = 1; i <= n; i++) {
if (G[i].size()) {
auto u = G[i][0];
q.push(node{ u.v,i,0,u.w });
}
}
qlimit += 10; int tot = 0;
while (qlimit--) {
auto now = q.top(); q.pop();
ans[++tot] = now.d;
if (!G[now.u].empty()) {
q.push(node{ G[now.u][0].v,now.u,0,now.d + G[now.u][0].w });
}
if (G[now.la].size() > now.pos + 1) {
q.push(node{ G[now.la][now.pos + 1].v,now.la,now.pos + 1,now.d - G[now.la][now.pos].w + G[now.la][now.pos + 1].w });
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int te; cin >> te;
while (te--) {
cin >> n >> m >> q;
for (int i = 1; i <= n; i++)G[i].clear();
while (m--) {
int a, b; ll c;
cin >> a >> b >> c;
G[a].push_back(fuck{ b,c });
}
for (int i = 1; i <= n; i++)
sort(G[i].begin(), G[i].end(), cmp);
qlimit = 0;
for (int i = 1; i <= q; i++) {
cin >> nq[i];
qlimit = max(qlimit, nq[i]);
}
solve();
for (int i = 1; i <= q; i++) {
cout << ans[nq[i]] << "\n";
}
}
return 0;
}