UVA1416 Warfare And Logistics

本文介绍了一个算法问题,涉及计算敌方物流成本的变化。通过给定的图结构,首先计算所有节点间的最短路径总和,然后尝试删除每条边来观察总成本的变化,寻找能够使成本增加最多的边进行删除。

Warfare And Logistics

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu
[Submit] [Go Back] [Status] 

Description
Download as PDF
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(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 < 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

题意:给一个n个点m条边的图,求所有节点的最短路之和,然后删除一条边再求所有最短路之和,求这些中最大的。

先跑一遍最短路,跑完把每一条对应的边都枚举存起来(bel数组,sum数组存每个点的最短路),然后枚举删除,跑最短路就好了。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define ll long long
using namespace std;
const int MAXN=110;
const int MAXE=2010;
const int INF=999999999;
int n,m,l;
struct EDGE
{
    int v,next;
    int dis;
}edge[MAXE];
struct HeadNode
{
    int d,u;
    bool operator < (const HeadNode& rhs) const
    {
        return d>rhs.d;
    }
};
int head[MAXN],size,pre[MAXN];
ll sum[MAXN],ans0,ans,tmp;
bool bel[MAXN][MAXE],del[MAXE];
void init()
{
    memset(head,-1,sizeof(head));
    size=0;
}
void add_edge(int u,int v,int dis)
{
    edge[size].v=v;
    edge[size].dis=dis;
    edge[size].next=head[u];
    head[u]=size++;
}
int dist[MAXN];
bool vis[MAXN];
void dijkstra(int s)
{
    memset(pre,-1,sizeof(pre));
    memset(vis,0,sizeof(vis));
    priority_queue<HeadNode> q;
    for(int i=1;i<=n;i++)
        dist[i]=INF;
    dist[s]=0;
    sum[s]=0;
    q.push(HeadNode{0,s});
    while(!q.empty())
    {
        HeadNode x=q.top();
        q.pop();
        int u=x.u;
        if(vis[u])
            continue;
        vis[u]=1;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(dist[v]>dist[u]+edge[i].dis)
            {
                dist[v]=dist[u]+edge[i].dis;
                pre[v]=i;
                q.push(HeadNode{dist[v],v});
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(i!=s)
        {
            if(dist[i]<INF)
                bel[s][pre[i]]=bel[s][pre[i]^1]=1;
            else
                dist[i]=l;
            ans0+=dist[i];
            sum[s]+=dist[i];
        }
    }
}
void dijkstra1(int s)
{
    memset(vis,0,sizeof(vis));
    priority_queue<HeadNode> q;
    for(int i=1;i<=n;i++)
        dist[i]=INF;
    dist[s]=0;
    q.push(HeadNode{0,s});
    while(!q.empty())
    {
        HeadNode x=q.top();
        q.pop();
        int u=x.u;
        if(vis[u])
            continue;
        vis[u]=1;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            if(del[i])
                continue;
            int v=edge[i].v;
            if(dist[v]>dist[u]+edge[i].dis)
            {
                dist[v]=dist[u]+edge[i].dis;
                q.push(HeadNode{dist[v],v});
            }
        }
    }
    ll k=0;
    for(int i=1;i<=n;i++)
    {
        if(i!=s)
        {
            if(dist[i]==INF)
                dist[i]=l;
            k+=dist[i];
        }
    }
    tmp=tmp-sum[s]+k;
}
int main()
{
    int i;
    while(scanf("%d%d%d",&n,&m,&l)!=EOF)
    {
        int u,v,d;
        init();
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&d);
            add_edge(u,v,d);
            add_edge(v,u,d);
        }
        ans0=0;
        memset(bel,0,sizeof(bel));
        for(i=1;i<=n;i++)
            dijkstra(i);
        printf("%lld ",ans0);
        ans=ans0;
        memset(del,0,sizeof(del));
        for(int i=0;i<size;i+=2)
        {
            del[i]=del[i^1]=1;
            tmp=ans0;
            for(int j=1;j<=n;j++)
                if(bel[j][i])
                dijkstra1(j);
            del[i]=del[i^1]=0;
            ans=max(ans,tmp);
        }
        printf("%lld\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值