UVA 1416 Warfare And Logistics 最短路树
对每个点求最短路,同时求出最短路树。
枚举每条边,如果这条边在最短路树上,那么删掉这条边就需要重新计算最短路,否则不需要。
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath> #include<climits> #include<string> #include<map> #include<queue> #include<vector> #include<stack> #include<set> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; #define pb(a) push(a) #define INF 0x1f1f1f1f #define lson idx<<1,l,mid #define rson idx<<1|1,mid+1,r #define PI 3.1415926535898 template<class T> T min(const T& a,const T& b,const T& c) { return min(min(a,b),min(a,c)); } template<class T> T max(const T& a,const T& b,const T& c) { return max(max(a,b),max(a,c)); } void debug() { #ifdef ONLINE_JUDGE #else freopen("d:\\in1.txt","r",stdin); freopen("d:\\out1.txt","w",stdout); #endif } int getch() { int ch; while((ch=getchar())!=EOF) { if(ch!=' '&&ch!='\n')return ch; } return EOF; } struct Edge { int from,to,dist; }; struct HeapNode { int d,u; bool operator < (const HeapNode &ant ) const { return d>ant.d; } }; const int maxn = 105; int n,m; vector<int> g[maxn]; vector<Edge> edge; int done[maxn]; void init() { for(int i=1;i<=n;i++) g[i].clear(); edge.clear(); } void add(int u,int v,int w) { Edge e=(Edge){u,v,w}; edge.push_back(e); g[u].push_back(edge.size()-1); } void dijksta(int s,int *d,int *p,int eid) { for(int i=1;i<=n;i++) d[i]=INF; memset(done,0,sizeof(done)); priority_queue<HeapNode> q; p[s]=-1; d[s]=0; q.push((HeapNode){0,s}); while(!q.empty()) { HeapNode x=q.top();q.pop(); int u=x.u; if(done[u])continue; done[u]=1; for(int i=0;i<g[u].size();i++) { if(g[u][i]==eid||g[u][i]==(eid^1))continue; Edge &e=edge[g[u][i]]; if(d[e.to]>d[u]+e.dist) { d[e.to]=d[u]+e.dist; p[e.to]=g[u][i]; q.push((HeapNode){d[e.to],e.to}); } } } } int d[maxn][maxn],p[maxn][maxn]; int L; long long sigma(int u) { long long c=0; for(int i=1;i<=n;i++) c=c+(d[u][i]==INF?L:d[u][i]); return c; } long long solve() { long long maxc=-1; for(int i=0;i<2*m;i+=2) { long long c=0; for(int j=1;j<=n;j++) { if(p[j][edge[i].to]==i||p[j][edge[i+1].to]==i+1) { dijksta(j,d[j],p[j],i); c+=sigma(j); dijksta(j,d[j],p[j],m*2); }else c+=sigma(j); } maxc=max(maxc,c); } return maxc; } int main() { while(scanf("%d%d%d",&n,&m,&L)!=EOF) { init(); for(int i=0;i<m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } for(int i=1;i<=n;i++) dijksta(i,d[i],p[i],2*m); long long c=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) c=c+(d[i][j]==INF?L:d[i][j]); long long c1=solve(); printf("%lld %lld\n",c,c1); } return 0; }