HZOI That day she left me

Title Description

She walked in quietly, disappeared without a trace.
I still remember that period of time, we travel together, we traveled together landscape. To the final points of interest, she quietly disappeared, leaving me alone and return.
I still remember, that can be expressed as a tourist area by the nn the n-nodes mm m edges consisting of an undirected graph. My return visit, but found himself just as soon as the end of this tour. From my point of departure scenic (ie No. 1 node), but just want to find the shortest of a loop back to the starting point, and is not repeated midway through any of its sides.
That is: I wanted to find a small ring from the starting point to the starting point.

Input Format

Each test point plurality of sets of test data
of the first row there is a positive integer T , ( T 10 ) represents the number of data sets
the following for each set of data, the first row there are two positive integers  n- , m , ( n- , m 10 . 4) represent the points and edges FIGS
Then there mm m rows, each row of three integers U, v, du, v, D U , v , D represents U, Vu, v U , between v a length of  D , ( D 10 . 3 ) path
to ensure the absence of heavy edges from the ring.

Output Format

For each test case, the output of the required length of the title of the smallest ring.
No Solution Output -1

 

background

  After reading this question I have come to one conclusion: hypocritical people do not have a girlfriend.

  Once on shore, I thought I could cut off t1 t2, but in fact. . . .

  t2 is the crumbs.

  This question lacks difficulty thinking, focusing on writing code.
 
  I am a tune.

Ideas analysis

  Although this title lacks difficulty thinking, but not ideas or follow me comb it again.

  First, we read the title found this problem requires a minimum ring.

  ? ? ? ? ? I never learned that God Benben algorithm, How about you?

  However, the question of people not busy wise to create their own algorithms to test you.

  So this must be learned algorithms.

  What about the algorithm with the smallest ring is it?

  emmmm. . . Graph Theory ah? Shortest?

  Talking about graph theory, the most basic is a variety of the most Shortest Path, what sister Stella Di ah, ah dead Pa law, what Sigmund easy to get ah? Graph theory are all most familiar with.

  Most (top) √

  Short (small) √

  Road (cyclo) ×

  Good, matching the two, surely there must be the relationship between the two [gab

  Then we take a closer look at the original words is how to say

      Find a small ring from the starting point to the starting point.

  Since the start (end) has been determined, then we might draw a diagram to look at.

  

  This is my hard hand painted Figure ah, HZ conditions are good hard ah, I have a drawing tool before painting faster and better, ah, hard

  We look good this picture, assuming that the right side is 1.

  So how can you find {1,2, 4,5} this collection it?

  We found that, in fact, we just run the shortest path between the 2 and 4, plus 1 is this collection.

  Then we look at a few pictures and then draw.

  。。。。。。。。。。。。。

  。。。。。。。。。。。。。

    I have not found a conclusion?

    Shortest run on node 1 is connected to the one and only one shortest path is contained in the smallest ring. [Equal to what is negligible

  Then the length of the smallest ring, is this plus the length of the shortest distance between two nodes 1.

  Well, let's try to use it this conclusion.

  We record and all nodes connected to 1, the presence yh [i] array.

  Built side when connected directly to the side and 1 out of all the ban.

  Then the shortest were recorded between every two, plus the distance between yh 1 and, taking a minimum, that is the answer.

  Note here that it is best not SPFA.


 

Details

  When you finish above approach when, well, the sample is passed over , but not actually, you TLE up.

  ??????????

  ??????????

  Algorithm did wrong ah? Where exactly is the problem?

  仔细想想,复杂度不太对啊?最大可能 n2logn ?【如果不对请留言告诉我

  这不就完全炸掉了吗?

  那怎么办啊?

  这个时候来仔细想想,究竟是什么地方出现了问题呢?

  由于建的是双向边,那么我们跑 最短路的时候很可能就不断的在重复。

  怎么样才能解决掉这个问题呢?

  这里介绍一个玄学优化 【正常人想不到就很正常

  我们对于每一个与1相连的点进行 二进制拆分

  这样我们的点就可以被被分成好多个两组。

  一组做起点,一组做终点,在这两组之间跑最短路。

  写对就AC了,加油吧!


 

代码实现

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;

struct ss{
    int to,nxt,val;
}jd[80010];
int t,n,m,dis[10010],yh[10010],cnt;
int head[10010],ecnt,vis[10010],dis1[10010],ans;
int cnt1,ed[10010];
priority_queue<pair<int,int> >q;

inline void add(int a,int b,int c) { jd[++ecnt].to=b; jd[ecnt].nxt=head[a]; jd[ecnt].val=c; head[a]=ecnt; return; } void clear() { cnt=0; ecnt=0; memset(head,0,sizeof head); memset(dis,0x3f,sizeof dis); memset(yh,0,sizeof yh); ans=100000000; return; } void dij() { while(!q.empty()) { while(vis[q.top().second]&&!q.empty()) q.pop(); if(q.empty()) break; int u=q.top().second; q.pop(); vis[u]=1; for(int v=head[u];v;v=jd[v].nxt) { int y=jd[v].to; int z=jd[v].val; if(vis[y]) continue; if(dis1[y]>dis1[u]+z) { dis1[y]=dis1[u]+z; q.push(make_pair(-dis1[y],y)); } } } return; } int main() { scanf("%d",&t); while(t--) { clear(); scanf("%d%d",&n,&m); int x,y,z; for(int i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&z); if(x==1||y==1){ if(x==1) swap(x,y); yh[++cnt]=x; dis[x]=z; } else{ add(x,y,z); add(y,x,z); } } int k=0; while((1<<k)<=n) k++; for(int i=0;i<k;i++) { memset(dis1,0x3f,sizeof dis1); memset(vis,0,sizeof vis); memset(ed,0,sizeof ed); cnt1=0; while(!q.empty()) q.pop(); for(int j=1;j<=cnt;j++) { if(((1<<i)&yh[j])) { q.push(make_pair(-dis[yh[j]],yh[j])); dis1[yh[j]]=dis[yh[j]]; } else{ ed[++cnt1]=yh[j]; } } dij(); for(int j=1;j<=cnt1;j++) ans=min(ans,dis1[ed[j]]+dis[ed[j]]); } if(ans<100000000) printf("%d\n",ans); else printf("-1\n"); } return 0; }

AC啦!!!!!!!!!!

Guess you like

Origin www.cnblogs.com/qxyzili--24/p/11220786.html