uvalive 4080 Warfare And Logistics(最短路树)

来源:互联网 发布:诺基亚925下载软件 编辑:程序博客网 时间:2024/06/05 06:51

题目链接

Warfare And Logistics

Time Limit: 3000ms
Memory Limit: 131072KB
This problem will be judged on UVALive. Original ID: 4080
64-bit integer IO format: %lld      Java class name: Main
Prev 
Submit Status Statistics Discuss
 Next
Type: 
None
     

    [PDF Link]

    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 = $ \sum^{{n}}_{{i=1}}$$ \sum^{{n}}_{{j=1}}$path(ij) . Here path(ij) is the shortest path between locations i and j . In case i and j are not connected, path(ij) = 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 < n$ \le$100 , 1$ \le$m$ \le$1000 , 1$ \le$L$ \le$10$\scriptstyle \wedge$8 . 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  10001  3  21  4  42  1  32  3  33  4  14  2  2

    Sample Output

    28  38

    Source

    Regionals 2007, Asia - Changchun


    刘汝佳训练指南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;}

    0 0
    原创粉丝点击