题目链接
Warfare And Logistics
64-bit integer IO format: %lld Java class name: Main
The army of United Nations launched a new wave of air strikes on terrorist forces. The objective of the mission is to reduce enemy's logistical mobility. Each air strike will destroy a path and therefore increase the shipping cost of the shortest path between two enemy locations. The maximal damage is always desirable.
Let's assume that there are n enemy locations connected by m bidirectional paths, each with specific shipping cost. Enemy's total shipping cost is given as c = path(i, j) . Here path(i, j) is the shortest path between locations i and j . In case i and j are not connected, path(i, j) = L . Each air strike can only destroy one path. The total shipping cost after the strike is noted as c' . In order to maximized the damage to the enemy, UN's air force try to find the maximal c' - c .
Input
The first line ofeach input case consists ofthree integers: n , m , and L . 1 < n100 , 1m1000 , 1L108 . Each ofthe following m lines contains three integers: a , b , s , indicating length of the path between a and b .
Output
For each case, output the total shipping cost before the air strike and the maximal total shipping cost after the strike. Output them in one line separated by a space.
Sample Input
4 6 1000 1 3 2 1 4 4 2 1 3 2 3 3 3 4 1 4 2 2
Sample Output
28 38
Source
刘汝佳训练指南P330上例题,运用了最短路树。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN=100+20;
const int MAXM=1000*2+100;
const int INF=0x3f3f3f3f;
struct Edge
{
int from,to,next,w;
}edge[MAXM];
int head[MAXN],tol;
int d[MAXN][MAXN],pre[MAXN][MAXN];
int n,m,l;
void init()
{
tol=0;
memset(pre,0,sizeof(pre));
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{
edge[tol].from=u,edge[tol].to=v,edge[tol].w=w,edge[tol].next=head[u],head[u]=tol++;
edge[tol].from=v,edge[tol].to=u,edge[tol].w=w,edge[tol].next=head[v],head[v]=tol++;
}
void spfa(int s)
{
int q[MAXN],inq[MAXN];
memset(q,0,sizeof(q));
memset(inq,0,sizeof(inq));
for(int i=1;i<=n;i++) d[s][i]=INF;
int front=0,rear=0;
q[rear++]=s,d[s][s]=0;
inq[s]=1;
while(front!=rear)
{
int u=q[front++];
if(front>=MAXN) front=0;
inq[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(d[s][v]>d[s][u]+edge[i].w)
{
pre[s][v]=u;
d[s][v]=d[s][u]+edge[i].w;
if(inq[v]) continue;
inq[v]=1;
q[rear++]=v;
if(rear>=MAXN) rear=0;
}
}
}
for(int i=1;i<=n;i++)
if(d[s][i]>=INF) d[s][i]=l;
}
int a[MAXN];
int main()
{
while(~scanf("%d%d%d",&n,&m,&l))
{
init();
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
for(int i=1;i<=n;i++) spfa(i);
int ans=0;
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
a[i]+=d[i][j],ans+=d[i][j];
printf("%d ",ans);
for(int i=0;i<tol;i+=2)
{
int res=0;
int u=edge[i].from,v=edge[i].to;
for(int j=1;j<=n;j++)
{
if(pre[j][u]!=v&&pre[j][v]!=u)
{
res+=a[j];
continue;
}
int w=edge[i].w;
edge[i].w=INF,edge[i^1].w=INF;
spfa(j);
int re=0;
for(int k=1;k<=n;k++) re+=d[j][k];
res+=re;
edge[i].w=w,edge[i^1].w=w;
spfa(j);
}
ans=max(ans,res);
}
printf("%d\n",ans);
}
return 0;
}