UVa4080 Warfare And Logistics
来源:互联网 发布:php时间戳精确到毫秒 编辑:程序博客网 时间:2024/06/05 04:10
题目描述 传送门
尝试删
但对于点
预处理好每个点的最短路树,这样最多计算
坑点:要用第二小的边代替删去的边。
代码
#include<cstdio>#include<iostream>#include<algorithm>#include<queue>#include<cstring>using namespace std;const int INF=1e9;const int maxn=104,maxm=1005;int h[maxn],d[maxn],sum[maxn],p[maxn],secedge[maxn][maxn],edgeid[maxn][maxn];bool vis[maxn],need[maxn][maxm*2];struct Edge{ int t,d,nxt; Edge(int a=0,int b=0,int c=0):t(a),d(b),nxt(c){}}edges[maxm*2];int n,m,cnt,L;struct heap{ int d,v; heap(int a=0,int b=0):d(a),v(b){} bool operator<(const heap&a)const{ return d>a.d; }};void addedge(int from,int to,int dist){ edges[++cnt]=Edge(to,dist,h[from]); h[from]=cnt; edges[++cnt]=Edge(from,dist,h[to]); h[to]=cnt; edgeid[from][to]=edgeid[to][from]=cnt;}int main(){ while(scanf("%d%d%d",&n,&m,&L)!=EOF){ cnt=-1; memset(h,-1,sizeof(h)); memset(secedge,0,sizeof(secedge)); for(int i=0;i<m;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); if(!secedge[a][b]){ addedge(a,b,c); secedge[a][b]=secedge[b][a]=INF; } else if(c<edges[edgeid[a][b]].d){ secedge[a][b]=secedge[b][a]=edges[edgeid[a][b]].d; edges[edgeid[a][b]].d=edges[edgeid[a][b]^1].d=c; } else if(c<secedge[a][b]) secedge[a][b]=secedge[b][a]=c; } memset(sum,0,sizeof(sum)); memset(need,0,sizeof(need)); int c=0; for(int i=1;i<=n;i++){ memset(vis,0,sizeof(vis)); priority_queue<heap>q; q.push(heap(0,i)); for(int i=1;i<=n;i++) d[i]=INF; d[i]=0; while(!q.empty()){ heap x=q.top();q.pop(); int u=x.v; if(vis[u]) continue; vis[u]=1; for(int i=h[u];i>-1;i=edges[i].nxt){ int v=edges[i].t; if(d[u]+edges[i].d<d[v]){ d[v]=d[u]+edges[i].d; p[v]=i; q.push(heap(d[v],v)); } } } for(int j=1;j<=n;j++){ sum[i]+=d[j]==INF?L:d[j]; if(j!=i) need[i][p[j]]=need[i][p[j]^1]=1; } c+=sum[i]; } int ans=0; for(int i=0;i<2*m;i+=2){ int c1=0; for(int j=1;j<=n;j++){ if(need[j][i]){ swap(secedge[edges[i].t][edges[i^1].t],edges[i].d); edges[i^1].d=edges[i].d; memset(vis,0,sizeof(vis)); priority_queue<heap>q; q.push(heap(0,j)); for(int i=1;i<=n;i++) d[i]=INF; d[j]=0; while(!q.empty()){ heap x=q.top();q.pop(); int u=x.v; if(vis[u]) continue; vis[u]=1; for(int i=h[u];i>-1;i=edges[i].nxt){ int v=edges[i].t; if(d[u]+edges[i].d<d[v]){ d[v]=d[u]+edges[i].d; q.push(heap(d[v],v)); } } } for(int k=1;k<=n;k++) c1+=d[k]==INF?L:d[k]; swap(secedge[edges[i].t][edges[i^1].t],edges[i].d); edges[i^1].d=edges[i].d; } else c1+=sum[j]; } ans=max(ans,c1); } printf("%d %d\n",c,ans); } return 0;}
阅读全文
0 0
- UVa4080 Warfare And Logistics
- LA4080 Warfare And Logistics
- UVA1416 Warfare And Logistics
- UVA1416 Warfare And Logistics
- UVA1416 Warfare And Logistics(LA4080)
- LA 4080 Warfare And Logistics
- uva 1416 - Warfare And Logistics(dijkstra)
- la4080 Warfare And Logistics 枚举+最短路
- UVA - 1416 Warfare And Logistics (最短路)
- LA 4080 warfare And Logistics (Dijkstra)
- La 4080 Warfare and logistics Dijkstra
- UVA1416 Warfare And Logistics(LA4080)(最短路,5级)
- Uva - 1416 Warfare And Logistics dijkstra维护..题目范围有误..
- UVALive 4080 Warfare And Logistics(最短路树)
- UVA 1416 - Warfare And Logistics(最短路树)
- uva1416 - Warfare And Logistics 最短路树优化
- uvalive 4080 Warfare And Logistics最短路树
- UVALive 4080 Warfare And Logistics(Dijkstra+最短路树)
- python reduce函数
- Mnist数据集分析(13)---《深度学习》
- Error:Execution failed for task ':app:transformClassesWithDexForAnzhiRelease'. > com.android.build.a
- js对象及DOM
- opencv-图像处理1 图像基本操作
- UVa4080 Warfare And Logistics
- 10月集训test9
- 集成学习的原理
- ConfigReader(五十八)—— ReadVipLevelConfig
- redux middleware 详解
- 浏览器-点击预览视频文件(自动播放、循环播放)
- 模仿cmd启动另一个程序
- 单链表中进行直接插入排序(有头节点)
- 激发偶尔;未决犯;