uvalive 4080 Warfare And Logistics最短路树
来源:互联网 发布:老人海边摸蛤 知乎 编辑:程序博客网 时间:2024/06/05 01:09
求删去一条边后,任意两点距离之和最大值。
对于每个节点它到其他点的最短路径存在一个最短路树,若不破坏这个最短路树,那么它到其余点的最短路径不变。所以本题只需对每个节点求n次最短路。
#include<iostream>#include<string>#include<cstring>#include<cstdio>#include<cmath>#include<iomanip>#include<map>#include<algorithm>#include<queue>#include<set>#define inf 1000000000000#define pi acos(-1.0)#define eps 1e-8#define seed 131using namespace std;typedef pair<int,int> pii;typedef unsigned long long ull;typedef long long ll;const int maxn=100005;int n,m;struct Edge{ int from,to,w; Edge(){} Edge(int a,int b,int c):from(a),to(b),w(c){}};struct node{ int w,id; node(int a,int b):w(a),id(b){} bool operator<(const node& a)const { return w>a.w; }};ll l;bool tree[105][2005];vector<Edge>edge;vector<int>g[105];ll cost[105];ll d[105];void dij(int s);ll dij2(int s,int p);int main(){ int a,b,c; while(~scanf("%d%d%lld",&n,&m,&l)) { edge.clear(); for(int i=1;i<=n;i++) g[i].clear(); for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); edge.push_back(Edge(a,b,c)); edge.push_back(Edge(b,a,c)); g[a].push_back(edge.size()-2); g[b].push_back(edge.size()-1); } memset(tree,0,sizeof(tree)); for(int i=1;i<=n;i++) dij(i); ll ans=0; for(int i=1;i<=n;i++) ans+=cost[i]; cout<<ans; ll mx=0; ll y; for(int i=0;i<m*2;i+=2) { y=0; for(int j=1;j<=n;j++) { if(tree[j][i]==1||tree[j][i+1]==1) { y+=dij2(j,i); } else y+=cost[j]; } mx=max(mx,y); } cout<<" "<<mx<<endl; } return 0;}void dij(int s){ bool vis[105]; int p[105]; memset(p,-1,sizeof(p)); memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) d[i]=inf; d[s]=0; priority_queue<node>que; que.push(node(0,s)); while(!que.empty()) { node u=que.top(); que.pop(); int len=g[u.id].size(); for(int i=0;i<len;i++) { Edge& e=edge[g[u.id][i]]; if(vis[e.to]==0&&d[e.to]>u.w+e.w) { d[e.to]=u.w+e.w; p[e.to]=g[u.id][i]; que.push(node(d[e.to],e.to)); } } } for(int i=1;i<=n;i++) { if(i==s||p[i]==-1) continue; tree[s][p[i]]=1; } cost[s]=0; for(int i=1;i<=n;i++) { if(d[i]==inf) cost[s]+=l; else cost[s]+=d[i]; }}ll dij2(int s,int p){ bool vis[105]; memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) d[i]=inf; d[s]=0; priority_queue<node>que; que.push(node(0,s)); while(!que.empty()) { node u=que.top(); que.pop(); vis[u.id]=1; int len=g[u.id].size(); for(int i=0;i<len;i++) { if(g[u.id][i]==p||g[u.id][i]==p+1) continue; Edge& e=edge[g[u.id][i]]; if(vis[e.to]==0&&d[e.to]>u.w+e.w) { d[e.to]=u.w+e.w; que.push(node(d[e.to],e.to)); } } } ll ans=0; for(int i=1;i<=n;i++) { if(d[i]==inf) ans+=l; else ans+=d[i]; } return ans;}
0 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
- xcode5设置不用arc
- 判断单链表里面有没有环
- 变态跳台阶
- 阿里笔试题记录(Android客户端部分)
- vlc-android源码阅读笔记之视频播放器界面
- uvalive 4080 Warfare And Logistics最短路树
- 用Markdown语法统一为知笔记和CSDN博客
- Spring事务管理原理
- uva10815 Andy's First Dictionary(字符串的简单处理)
- 2015深圳电子展-第85届中国电子展概况
- 关于wamp不支持php中GD库的问题
- 历届试题 买不到的数目
- kmp
- 深入理解OAuth2.0协议