bzoj2180: 最小直徑產生樹

來源:互聯網
上載者:User

傳送門:http://www.lydsy.com/JudgeOnline/problem.php?id=2180

思路:先枚舉圖的絕對中心在哪條邊(u,v)上,絕對中心就是到最遠點的距離最近的點,可以在邊上

設絕對中心到該邊一個端點的距離為x

然後每個圖中的s點到它的距離就會是關於x的函數 即min(dis[u][s]+x,dis[v][s]+L-x)

這是一條折線,那麼每個點到它的距離都會是一條折線

那麼,我們就要使兩個距離最大點的取值最小

就是所有折線的最上面構成的折線的最下面的點

說的有些複雜,可以看這個部落格

http://blog.csdn.net/crazy_ac/article/details/8816877

按d[u][i]從大到小排序一遍掃描即可


#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>const int maxn=205,maxm=50010;using namespace std;int n,m,d[maxn][maxn],inf,tot,rank[maxn][maxn],ans=(int)1e9;struct Edge{int x,y,v;}E[maxm];void floyd(){for (int k=1;k<=n;k++)for (int i=1;i<=n;i++) if (i!=k)for (int j=1;j<=n;j++) if (i!=j&&k!=j)d[i][j]=min(d[i][j],d[i][k]+d[k][j]);}void work(int id){int u=E[id].x,v=E[id].y,L=E[id].v;for (int a=1,b=2;b<=n;b++){if (d[v][rank[u][a]]>d[v][rank[u][b]]) continue;ans=min(ans,d[v][rank[u][a]]+d[u][rank[u][b]]+L);a=b;}}int main(){scanf("%d%d",&n,&m),memset(d,63,sizeof(d)),inf=d[0][0];for (int i=1;i<=n;i++) d[i][i]=0;for (int i=1,x,y,z;i<=m;i++) scanf("%d%d%d",&x,&y,&z),d[x][y]=d[y][x]=min(d[x][y],z);//for (int i=1;i<=n;i++,puts("")) for (int j=1;j<=n;j++) printf("%d ",d[i][j]);for (int i=1;i<=n;i++) for (int j=1;j<i;j++) if (d[i][j]!=inf) E[++tot]=(Edge){j,i,d[i][j]};floyd();for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) rank[i][j]=j;for (int i=1;i<=n;i++)for (int j=1;j<=n;j++)for (int k=j+1;k<=n;k++)if (d[i][rank[i][j]]<d[i][rank[i][k]]) swap(rank[i][j],rank[i][k]);for (int i=1;i<=n;i++) ans=min(ans,d[i][rank[i][1]]+d[i][rank[i][2]]);for (int i=1;i<=tot;i++) work(i);printf("%d\n",ans);return 0;}/*8 111 3 11 2 22 4 32 6 74 6 06 8 48 7 37 5 16 3 57 3 55 3 6*/


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.