Acdream 1415 Shortest circuit + bridge

Source: Internet
Author: User

Click to open link

Test instructions: For a picture, ask you how many bridges are on the shortest path from 1 to N.

Idea: first to meet the conditions of the shortest circuit, then judge each edge is not the shortest side of the edge, how to judge is also very simple, starting from 1 to find the shortest and starting from N to find the fastest way, for the edge U to V, if the most short-circuit of 1 to u plus the shortest short of N to V in addition to the weight of this edge if equal to 1 to N, So this side is what we want, that's the condition if (dis1[u[i]]+cost[i]+dis2[v[i]]==maxdis| | Dis1[v[i]]+cost[i]+dis2[u[i]]==maxdis) Maxdis is 1 to n the shortest short length, and then the topic said there may be heavy side, good processing, before the teammate told me test instructions said no heavy side, WA for a long time, Then the ID and the number of edges to find the output, just the application of the template, it is not difficult ~ ~ ~

#include <queue> #include <vector> #include <stdio.h> #include <stdlib.h> #include <string.h > #include <iostream> #include <algorithm>using namespace Std;typedef long Long ll;const ll inf=    0x3f3f3f3f3f3f3f3fll;const int maxn=20010;struct edge{int to,cost; Edge () {} edge (int a,int b) {to=a;cost=b;}}; typedef pair<int,long LONG&GT;P;VECTOR&LT;EDGE&GT;G[MAXN];LL dis1[maxn],dis2[maxn];void Dijkstra (int st,ll dis[])    {priority_queue<p,vector<p>,greater<p> >que;    Fill (Dis,dis+maxn,inf);    dis[st]=0;    Que.push (P (0,st));        while (!que.empty ()) {P p=que.top (); Que.pop ();        int V=p.second;        if (Dis[v]<p.first) continue;            for (unsigned int i=0;i<g[v].size (); i++) {Edge e=g[v][i];                if (dis[e.to]>dis[v]+ (LL) e.cost) {dis[e.to]=dis[v]+ (ll) e.cost;            Que.push (P (dis[e.to],e.to));   }}}}int u[100010],v[100010],cost[100010];struct edge1{ int to,id,num; Edge1 (int a,int b,int c) {to=a;id=b;num=c;}}; Vector<edge1>gg[maxn];int l[maxn],e[maxn],ans[maxn],vis[maxn],stack1[maxn];int k,kk,cnt,n,m;void dfs (int x, int fa) {vis[x]=1; L[x]=k;    E[x]=k++;stack1[kk++]=x;    int flag=0;        for (unsigned int i=0;i<gg[x].size (); i++) {edge1 e=gg[x][i];                if (E.TO!=FA) {if (!vis[e.to]) {DFS (e.to,x);                L[x]=min (l[x],l[e.to]);            if (l[e.to]>e[x]) gg[x][i].num=1;        }else l[x]=min (l[x],e[e.to]);            }else{if (flag) l[x]=min (l[x],e[e.to]);        flag++;            }} if (L[x]==e[x]) {while (stack1[kk]!=x&&kk>0) {l[stack1[kk-1]]=l[x];            kk--;        vis[stack1[kk]]=0;    }}}void Tarjan () {Kk=0;k=1;dfs ();    cnt=0;            for (int i=1;i<=n;i++) {for (unsigned int j=0;j<gg[i].size (); j + +) {edge1 e=gg[i][j]; if (l[i]!=l[e.to]&&e.num==1) {ANS[CNT++]=e.id;        }}}}int Main () {while (scanf ("%d%d", &n,&m)!=-1) {for (int i=0;i<maxn;i++) g[i].clear ();        for (int i=0;i<maxn;i++) gg[i].clear ();            for (int i=0;i<m;i++) {scanf ("%d%d%d", &u[i],&v[i],&cost[i]);            G[u[i]].push_back (Edge (v[i],cost[i));        G[v[i]].push_back (Edge (u[i],cost[i));        } Dijkstra (1,DIS1);        Dijkstra (N,DIS2);        ll Maxdis=dis1[n]; for (int i=0;i<m;i++) {if (dis1[u[i]]+cost[i]+dis2[v[i]]==maxdis| |                Dis1[v[i]]+cost[i]+dis2[u[i]]==maxdis) {gg[u[i]].push_back (Edge1 (v[i],i+1,0));            Gg[v[i]].push_back (Edge1 (u[i],i+1,0));        }} Tarjan ();        printf ("%d\n", CNT);        Sort (ans,ans+cnt);        for (int i=0;i<cnt-1;i++) printf ("%d", ans[i]);    printf ("%d\n", ans[cnt-1]); } return 0;}

Acdream 1415 Shortest circuit + bridge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.