Hdu 3986 【枚举】【最短路】 解题报告

来源:互联网 发布:双敏主板上的网络精灵 编辑:程序博客网 时间:2024/06/04 18:32

Problem Description

The final battle is coming. Now Harry Potter is located at city 1, and Voldemort is located at city n. To make the world peace as soon as possible, Of course, Harry Potter will choose the shortest road between city 1 and city n. But unfortunately, Voldemort is so powerful that he can choose to destroy any one of the existing roads as he wish, but he can only destroy one. Now given the roads between cities, you are to give the shortest time that Harry Potter can reach city n and begin the battle in the worst case.

Input

First line, case number t (t<=20).
Then for each case: an integer n (2<=n<=1000) means the number of city in the magical world, the cities are numbered from 1 to n. Then an integer m means the roads in the magical world, m (0< m <=50000). Following m lines, each line with three integer u, v, w (u != v,1 <=u, v<=n, 1<=w <1000), separated by a single space. It means there is a bidirectional road between u and v with the cost of time w. There may be multiple roads between two cities.

Output

Each case per line: the shortest time to reach city n in the worst case. If it is impossible to reach city n in the worst case, output “-1”.

思路

删除的边一定是最短上的边,才会使得最坏情况下的路径最短。所以需要删除最短路的边后枚举其他边的最短路。这题和hdu3986差不多,这题中边没有重边,而hdu3986有重边。保存路径是要注意。

我用的是spfa。也可以用dij+优先队列。

代码

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>#include<vector>#include<queue>using namespace std;const int N=1000+5;const int M=100000+5;int ans=0,T,num_dot,side,num,flag;int pd[N],pre[N],die[M],dis[N],head[N];  struct node  {      int v,next,w,co;  };node ed[M];  void build(int u,int v,int w)  {      ed[num].v=v;      ed[num].w=w;      ed[num].next=head[u];      head[u]=num++;  }  void init()  {      int s,e,w;      num=0,flag=1;      memset(head,-1,sizeof(head));      memset(die,0,sizeof(die));      scanf("%d%d",&num_dot,&side);      for (int i=0;i<side;i++)      {          scanf("%d%d%d",&s,&e,&w);          ed[num].co=num+1;          build(s,e,w);          ed[num].co=num-1;          build(e,s,w);      }  }  void spfa()  {      int mid;      bool iq[N];      queue<int> q;      memset(iq,0,sizeof(iq));      memset(dis,127,sizeof(dis));      dis[1]=0;q.push(1);iq[1]=1;      while(q.size())      {          mid=q.front();          q.pop();          iq[mid]=0;          for (int i=head[mid];i>-1;i=ed[i].next)          if (die[i]==0&&dis[ed[i].v]>dis[mid]+ed[i].w)          {              dis[ed[i].v]=dis[mid]+ed[i].w;              if (flag)  {pd[ed[i].v]=mid;pre[ed[i].v]=i;}              if (!iq[ed[i].v]) {iq[ed[i].v]=1;q.push(ed[i].v);}          }      }      flag=0;  }  int main()  {      scanf("%d",&T);      while(T--)      {          init();          spfa();          if(dis[num_dot]>50000000) printf("-1\n");          else          {              ans=0;              for(int i=num_dot;i>1;i=pd[i])              {                  die[pre[i]]=1;                  die[ed[pre[i]].co]=1;                  spfa();                  ans=max(ans,dis[num_dot]);                  die[pre[i]]=0;                  die[ed[pre[i]].co]=0;              }              if(ans>50000000)  printf("-1\n");              else  printf("%d\n",ans);          }      }      return 0;}  
原创粉丝点击