uvalive 4080 Warfare And Logistics(最短路树)
来源:互联网 发布:诺基亚925下载软件 编辑:程序博客网 时间:2024/06/05 06:51
题目链接
Warfare And Logistics
64-bit integer IO format: %lld Java class name: Main
[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 = 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 10001 3 21 4 42 1 32 3 33 4 14 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;}
- UVALive 4080 Warfare And Logistics(最短路树)
- uvalive 4080 Warfare And Logistics最短路树
- UVALive 4080 Warfare And Logistics(Dijkstra+最短路树)
- UVALive - 4080 Warfare And Logistics (SPFA+最短路树)
- uvalive 4080 Warfare And Logistics(最短路树)
- UVA 1416 - Warfare And Logistics(最短路树)
- uva1416 - Warfare And Logistics 最短路树优化
- UVA-1416-Warfare And Logistics(最短路树)
- UVALive4080[Warfare And Logistics] 最短路树+dijkstra
- LA4080 Warfare And Logistics (dijkstra+最短路树)
- 10-22 warfare and logistics(最短路树)
- la4080 Warfare And Logistics 枚举+最短路
- UVA - 1416 Warfare And Logistics (最短路)
- LA 4080 Warfare And Logistics 战争和物流 dijkstra 最短路树
- UVA1416 Warfare And Logistics(LA4080)(最短路,5级)
- uva 1416 - Warfare And Logistics(最短路)
- uva1410题解Warfare And Logistics(LA4080)(最短路)
- LA 4080 Warfare And Logistics
- Java异常处理
- IDF16:解读Ceph百万IOPS测试及优化
- C#之WinForm基础 文件对话框内只可以看到的某一种类型
- [Android]Android Fragment 真正的完全解析(上)
- 安装 SQL Server 2014 Express
- uvalive 4080 Warfare And Logistics(最短路树)
- 2016年9月13-9月18日
- 【Web前端】HTML5 Canvas小游戏
- [Android]Android Fragment 真正的完全解析(下)
- Makefile中的 export 理解
- 走进blas/lapack(3)--cblas
- 【趣题&输入略坑】【并查集(带权)】NKOJ 3762 守夜人
- maven与spring的配置
- 详细解读XMLHttpRequest(一)同步请求和异步请求