Ultraviolet A 1416-warfare and logistics (Shortest Path Tree)

Source: Internet
Author: User
Va 1416-warfare and Logistics

Question Link

Given an undirected graph, each side has a positive weight, and C is equal to the sum of the shortest lengths of the two vertices. What is the maximum C value of the new graph after deleting one side?

Idea: It is too complicated to directly enumerate and delete edges. If the Shortest Path Tree is built, if the edges are deleted in the shortest path tree, they need to be done, in this way, the complexity is optimized to (n ^ 2 mlog (n), barely acceptable

Code:

#include <cstdio>#include <cstring>#include <vector>#include <queue>using namespace std;#define INF 0x3f3f3f3f3f3f3ftypedef long long ll;const int MAXNODE = 105;struct Edge {int u, v;ll dist;Edge() {}Edge(int u, int v, ll dist) {this->u = u;this->v = v;this->dist = dist;}};struct HeapNode {ll d;int u;HeapNode() {}HeapNode(ll d, int u) {this->d = d;this->u = u;}bool operator < (const HeapNode& c) const {return d > c.d;}};struct Dijkstra {int n, m;vector<Edge> edges;vector<int> g[MAXNODE];bool done[MAXNODE];ll d[MAXNODE];int p[MAXNODE];void init(int tot) {n = tot;for (int i = 0; i < n; i++)g[i].clear();edges.clear();}void add_Edge(int u, int v, ll dist) {edges.push_back(Edge(u, v, dist));m = edges.size();g[u].push_back(m - 1);}void print(int e) {//shun xuif (p[e] == -1) {printf("%d", e + 1);return;}print(edges[p[e]].u);printf(" %d", e + 1);}void print2(int e) {//ni xuif (p[e] == -1) {printf("%d\n", e + 1);return;}printf("%d ", e + 1);print2(edges[p[e]].u);}void dijkstra(int s) {priority_queue<HeapNode> Q;for (int i = 0; i < n; i++) d[i] = INF;d[s] = 0;p[s] = -1;memset(done, false, sizeof(done));Q.push(HeapNode(0, s));while (!Q.empty()) {HeapNode x = Q.top(); Q.pop();int u = x.u;if (done[u]) continue;done[u] = true;for (int i = 0; i < g[u].size(); i++) {Edge& e = edges[g[u][i]];if (d[e.v] > d[u] + e.dist) {d[e.v] = d[u] + e.dist;p[e.v] = g[u][i];Q.push(HeapNode(d[e.v], e.v));}}}}} gao, gao2;const int M = 1005;int n, m;ll L, c[M];Edge e[M];int main() {while (~scanf("%d%d%lld", &n, &m, &L)) {gao.init(n);int u, v;ll dist;for (int i = 0; i < m; i++) {scanf("%d%d%lld", &u, &v, &dist);u--; v--;e[i] = Edge(u, v, dist);gao.add_Edge(u, v, dist);gao.add_Edge(v, u, dist);}memset(c, 0, sizeof(c));ll ans1 = 0, ans2 = 0;for (int i = 0; i < n; i++) {gao.dijkstra(i);for (int j = 0; j < n; j++) {if (gao.d[j] == INF) ans1 += L;else ans1 += gao.d[j];}for (int j = 0; j < m; j++) {int u = e[j].u, v = e[j].v;if ((gao.p[v] != -1 && gao.edges[gao.p[v]].u == u) || ((gao.p[u] != -1) && gao.edges[gao.p[u]].u == v)) {gao2.init(n);for (int k = 0; k < m; k++) {if (j == k) continue;gao2.add_Edge(e[k].u, e[k].v, e[k].dist);gao2.add_Edge(e[k].v, e[k].u, e[k].dist);}gao2.dijkstra(i);for (int k = 0; k < n; k++) {if (gao2.d[k] == INF) c[j] += L;else c[j] += gao2.d[k];}} else {for (int k = 0; k < n; k++) {if (gao.d[k] == INF) c[j] += L;else c[j] += gao.d[k];}}}}for (int i = 0; i < m; i++)ans2 = max(ans2, c[i]);printf("%lld %lld\n", ans1, ans2);}return 0;}


Ultraviolet A 1416-warfare and logistics (Shortest Path Tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.