概述
题意:
设从1到每个点的最短距离为d,求删除几条边后仍然使1到每个点的距离为d,使得剩下的边最多为k
解析:
先求来一遍spfa,然后bfs遍历每条路,如果d[v] == d[u] + Node[i].w 则说明这条路要保留
注意是按着走的路的顺序输出的 wa1
注意最大值设为0x3f wa3 学到了。。。emm 用memset设置数组为0x3f是无穷大
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #include <limits.h> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define rb(a) scanf("%lf", &a) #define rf(a) scanf("%f", &a) #define pd(a) printf("%dn", a) #define plld(a) printf("%lldn", a) #define pc(a) printf("%cn", a) #define ps(a) printf("%sn", a) #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 2222222, INF = 0x7fffffff; int head[maxn], vis[maxn], nex[maxn]; LL d[maxn]; int cnt, n, m, s, t, k; vector<int> g; struct node { int u, v; LL w; }Node[maxn]; void add_(int u, int v, int w) { Node[cnt].u = u; Node[cnt].v = v; Node[cnt].w = w; nex[cnt] = head[u]; head[u] = cnt++; } void add(int u, int v, int w) { add_(u, v, w); add_(v, u, w); } int spfa() { mem(d, 0x3f); deque<int> Q; Q.push_front(s); d[s] = 0; vis[s] = 1; while(!Q.empty()) { int u = Q.front(); Q.pop_front(); vis[u] = 0; for(int i = head[u]; i != -1; i = nex[i]) { int v = Node[i].v; if(d[v] > d[u] + Node[i].w) { d[v] = d[u] + Node[i].w; if(!vis[v]) { if(Q.empty()) Q.push_front(v); else { if(d[v] < d[Q.front()]) Q.push_front(v); else Q.push_back(v); } vis[v] = 1; } } } } } void init() { mem(head, -1); g.clear(); cnt = 0; } void bfs() { queue<int> Q; Q.push(1); mem(vis, 0); vis[1] = 1; int cnt1 = 0; while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i = head[u]; i != -1; i = nex[i]) { int v = Node[i].v; if(d[v] == d[u] + Node[i].w) { if(!vis[v]) { g.push_back((i + 2) / 2); vis[v] = 1; Q.push(v); if(++cnt1 == k) return; } } } } } int main() { // cout << 0x3f <<endl; init(); int ans = 0; int u, v, w; cin >> n >> m >> k; for(int i = 0; i < m; i++) { cin >> u >> v >> w; add(u, v, w); } if(k == 0) return puts("0"); s = 1; spfa(); bfs(); cout << g.size() << endl; for(int i = 0; i < g.size(); i++) cout << g[i] << " "; cout << endl; return 0; }
转载于:https://www.cnblogs.com/WTSRUVF/p/9954585.html
最后
以上就是顺心皮皮虾为你收集整理的Edge Deletion CodeForces - 1076D(水最短路)的全部内容,希望文章能够帮你解决Edge Deletion CodeForces - 1076D(水最短路)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复