In Takahashi kingdom, which once existed, there is N cities, and some pairs of cities is connected bidirectionally b Y roads. The following is known about the road network:
- People traveled between cities only through roads. It is possible to reach any city from any other city, via intermediate cities if necessary.
- Different roads May has had Different lengths, but all the lengths were positive integers.
Snuke the archeologist found a table N N with rows and columns, A in the ruin of Takahashi kingdom. He thought that it represented the shortest distances between the cities along of the roads in the kingdom.
Determine whether there exists a road network such u v that for each and, the integer in the Au,v u -th row an d-th column of is equal to the length of the shortest path from city to City v A u v . If Such a network exist, find the shortest possible total length of the roads.
- 1≤N≤300
- If i≠j , 1≤Ai,j=Aj,i≤109 .
- Ai,i=0
Equivalent to the minimum cut to go to each unnecessary edge (in the matrix is a point) at each point to find other points of the shortest possible if the length is greater than the shortest path, delete if equal to the node to take the most
#include <math.h>#include<string.h>#include<iostream>#include<stdio.h>#include<algorithm>#include<sstream>//Istringstream stm (string); STM >> x;#include <vector>#include<queue>#defineINF 2139062143#defineinf-2139062144#definell Long Longusing namespacestd;Const intMAXN =1005; ll a[305][305];ll tempa[305][305];structEdge {ll from, to, Dist; Edge (ll u, LL V, ll D): from(U), to (v), Dist (d) {}};structHeapnode {ll d, u; BOOL operator< (ConstHeapnode &RHS)Const { returnD >RHS.D; }};structDijkstra {ll n, m; Vector<Edge>edges; Vector<ll>G[MAXN]; BOOLDONE[MAXN]; ll D[MAXN]; ll ROADCOUNT[MAXN]; ll P[MAXN]; //x?? ì?? Dμ?é?ò?ì???Vector<ll>path; voidInit (ll N) { This->n =N; for(LL i =0; I < n; i++) g[i].clear (); Edges.clear (); Path.clear (); } voidAddedge (LL from, LL to, LL Dist) {Edges.push_back (Edge ( from, to, Dist)); M=edges.size (); g[ from].push_back (M-1); } voidDijkstra (ll s) {//′ósμ?? Aê?Priority_queue <HeapNode>Q; for(LL i =0; I <= N; i++) D[i] =INF; for(LL i =0; I <= N; i++) Roadcount[i] =0; D[s]=0; memset (Done,0,sizeof(done)); Q.push ((heapnode) {0, S}); while(!Q.empty ()) {Heapnode x=Q.top (); Q.pop (); ll u=x.u; if(Done[u])Continue; Done[u]=true; for(LL i =0; I < g[u].size (); i++) {Edge&e =Edges[g[u][i]]; if(D[e.to] > D[u] +e.dist) {d[e.to]= D[u] +e.dist; Roadcount[e.to]= Roadcount[u] +1; P[e.to]=G[u][i]; Q.push ((Heapnode) {d[e.to], e.to}); } Else { if(d[e.to] = = D[u] +e.dist) {roadcount[e.to]= Max (Roadcount[e.to],roadcount[u] +1); } } } } } voidFindpath (ll X, ll end) {path.clear (); Path.push_back (x); FINDD (x, end); } voidFindd (ll X, ll end) {if(x = =end)return; Else{path.push_back ( This->edges[ This->P[X]]. from); FINDD ( This->edges[ This->P[X]]. from, end); } }};intMain () {ll n; scanf ("%lld",&N); Dijkstra DJ; Dj.init (n); for(LL i=0; i<n; i++) { for(LL j=0; j<n; J + +) {scanf ("%lld",&A[i][j]); TEMPA[I][J]=A[i][j]; if(I! =j) Dj.addedge (I,j,a[i][j]); } } BOOLFlag =true; for(LL i=0; I<n && Flag; i++) {Dj.dijkstra (i); for(LL j=0; j<n; J + +) { if(A[i][j] > dj.d[j]) flag =false; if(Dj.roadcount[j] >1) {Tempa[i][j]=0; Tempa[j][i]=0; } } } if(!flag) printf ("-1\n"); Else{ll Anss=0; for(LL i=0; i<n; i++) { for(LL j=0; j<n; J + +) {Anss+=Tempa[i][j]; }} printf ("%lld\n", ANSS/2); } return 0;}
Other
Atcoder Regular Contest 083 d:restoring Road Network