uvalive 4080 Warfare And Logistics(最短路树)

题目链接

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
Type: 
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •                   
  • [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  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;
    }

    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值