hdu 4679 Terrorist’s destroy (DP or 树直径,4级)

来源:互联网 发布:华为手机连不上4g网络 编辑:程序博客网 时间:2024/05/17 01:01

Terrorist’s destroy

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 132    Accepted Submission(s): 20


Problem Description
There is a city which is built like a tree.A terrorist wants to destroy the city's roads. But now he is alone, he can only destroy one road, then the city will be divided into two cities. Impression of the city is a number defined as the distance between the farthest two houses (As it relates to the fare).When the terrorist destroyed a road, he needs to spend some energy, assuming that the number is a.At the same time,he will get a number b which is maximum of the Impression of two cities. The terrorist wants to know which road to destroy so that the product of a and b will be minimized.You should find the road's id.
Note that the length of each road is one.
 

Input
The first line contains integer T(1<=T<=20), denote the number of the test cases.
For each test cases,the first line contains a integer n(1 < n <= 100000);denote the number of the houses;
Each of the following (n-1) lines contains third integers u,v,w, indicating there is a road between house u and houses v,and will cost terrorist w energy to destroy it.The id of these road is number from 1 to n-1.(1<=u<=n , 1<=v<=n , 1<=w<=10000)
 

Output
For each test case, output the case number first,and then output the id of the road which the terrorist should destroy.If the answer is not unique,output the smallest id.
 

Sample Input
254 5 11 5 12 1 13 5 151 4 11 3 15 1 12 5 1
 

Sample Output
Case #1: 2Case #2: 3
 

Source
2013 Multi-University Training Contest 8
 

Recommend
zhuyuanchen520

思路:找树直径,删边不在直径上,最长即是直径,在直径上,算切往直径剩下的最大长度。


#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<cstring>#include<cstdio>#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))#define  LL __int64using namespace std;const int mm=2e5+9;int head[mm],edge,n,dis[mm];class Edge{  public:int v,next,val,id;    bool yes;}e[mm];int ans,len;LL cun;void data(){  clr(head,-1);edge=0;}void add(int u,int v,int c,int id){ e[edge].id=id;e[edge].yes=0;  e[edge].v=v;e[edge].val=c;e[edge].next=head[u];head[u]=edge++;}void dfs(int u,int fa){  int v;  for(int i=head[u];~i;i=e[i].next)  {    v=e[i].v;    if(v==fa)continue;    if(dis[v]>dis[u]+1)    {dis[v]=dis[u]+1;     dfs(v,u);    }  }}void dfs1(int u,int fa){  int v;  for(int i=head[u];~i;i=e[i].next)  {    v=e[i].v;    if(v==fa)continue;    if(dis[v]+1==dis[u])    {      e[i].yes=1;e[i^1].yes=1;      dfs1(v,u);    }  }}void dfs2(int u,int fa){  int v,lu,lv;  for(int i=head[u];~i;i=e[i].next)  {    v=e[i].v;    if(v==fa)continue;      dfs2(v,u);      if(dis[u]>dis[v])      {        lv=dis[v];lu=len-dis[u];      }      else      {        lv=len-dis[v];lu=dis[u];      }    LL kz=max(lv,lu);    //printf("ld=%d %d %d\n",u,v,kz);    if(!e[i].yes)kz=len;    LL kv=e[i].val;    LL z=kv*kz;    if(ans==0)    {      ans=e[i].id;cun=z;    }    else if(z<cun)    {      ans=e[i].id;cun=z;    }    else if(z==cun&&e[i].id<ans)    {      ans=e[i].id;    }  }}void solve(){  clr(dis,0x3f);  dis[1]=0;  dfs(1,-1);  int kai=1;  FOR(i,1,n)///找树直径  if(dis[kai]<dis[i])kai=i;  clr(dis,0x3f);  dis[kai]=0;  dfs(kai,-1);//get longest  kai=1;  FOR(i,1,n)  if(dis[kai]<dis[i])kai=i;  len=dis[kai];  //cout<<" len="<<len<<endl;  dfs1(kai,-1);///标记最长  ans=0;  dfs2(kai,-1);  printf("%d\n",ans);}int main(){  int cas,a,b,c;  while(~scanf("%d",&cas))  {    FOR(ca,1,cas)    { data();      scanf("%d",&n);      FOR(i,1,n-1)      {        scanf("%d%d%d",&a,&b,&c);        add(a,b,c,i);add(b,a,c,i);      }      printf("Case #%d: ",ca);      solve();    }  }  return 0;}



原创粉丝点击