HDU - 6705 path (K短路+剪枝)

进入报名链接)~

path

Time 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.

Now there are q queries that you need to answer. Each of the queries is about the k-th minimum value of all the paths.

 

 

Input

The input consists of multiple test cases, starting with an integer t (1≤t≤100), denoting the number of the test cases.
The first line of each test case contains three positive integers n,m,q. (1≤n,m,q≤5∗104)

Each of the next m lines contains three integers ui,vi,wi, indicating that the i−th edge is from ui to vi and weighted wi.(1≤ui,vi≤n,1≤wi≤109)

Each of the next q lines contains one integer k as mentioned above.(1≤k≤5∗104)

It's guaranteed that Σn ,Σm, Σq,Σmax(k)≤2.5∗105 and max(k) won't exceed the number of paths in the graph.

 

 

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值