ACdream 1415 Important Roads(最短路&缩点&找桥)

Important Roads

Special Judge Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
Problem Description

      The city where Georgie lives has n junctions some of which are connected by bidirectional roads.
      Every day Georgie drives from his home to work and back. But the roads in the city where Georgie lives are very bad, so they are very often closed for repair. Georgie noticed that when some roads are closed he still can get from home to work in the same time as if all roads were available.

      But there are such roads that if they are closed for repair the time Georgie needs to get from home to work increases, and sometimes Georgie even cannot get to work by a car any more. Georgie calls such roads important.
      Help Georgie to find all important roads in the city.

Input

      The first line of the input file contains n and m — the number of junctions and roads in the city where Georgie lives, respectively (2 ≤ n ≤ 20 000, 1 ≤ m ≤ 100 000). Georgie lives at the junction 1 and works at the junction n.

      The following m lines contain information about roads. Each road is specified by the junctions it connects and the time Georgie needs to drive along it. The time to drive along the road is positive and doesn’t exceed 100 000. There can be several roads between a pair of junctions, but no road connects a junction to itself. It is guaranteed that if all roads are available, Georgie can get from home to work.

Output
      Output l — the number of important roads — at the first line of the output file. The second line must contain l numbers, the numbers of important roads. Roads are numbered from 1 to m as they are given in the input file.
Sample Input
6 7
1 2 1
2 3 1
2 5 3
1 3 2
3 5 1
2 4 1
5 6 2
Sample Output
2
5 7


备注:先 dijkstra()寻找每个点距起点的最短距离,再BFS一遍存一个起点到终点的最短路新图,之后tarjan()缩点,然后找桥。 图论算法大集合啊!!


代码

#define INF 0x7fffffff
#define eps (1e-9)
#define maxn 1000000000
#define clearto(s,x) memset(s,x,sizeof(s))
using namespace std;
typedef long long llint;

int n,m;
int dis[20009];
bool vis[20009];
typedef struct p{
    int to,id,val;
    bool operator < (const p &a)const
   {   return val>a.val;  }                //取时间最少优先
}   node;
vector<node>  rod[20009];

void dijkstra(){
    int i;
    priority_queue<node, vector<node> > que;
    clearto(vis,0);
    for(i=1;i<=n;i++)   {   dis[i]=INF;    }
    node nw,nxt;
    nw.to=1;   nw.val=dis[1]=0;     que.push(nw);
    while(!que.empty()){
       nw=que.top();        que.pop();
       int fa=nw.to;
       if(vis[fa]) continue;    vis[fa]=1;
       for(i=0;i<rod[fa].size();i++){
           int chl=rod[fa][i].to;
           if(dis[chl]>dis[fa]+rod[fa][i].val){
              dis[chl]=dis[fa]+rod[fa][i].val;
              nxt.to=chl;  nxt.val=dis[chl];
              que.push(nxt);
           }
       }
    }
}
vector<node> mps[20009];
void bfs(){
    queue<int> q;      q.push(n);
    clearto(vis,0);    vis[n]=1;
    node nxt;
    int i;
    while(!q.empty()){
        int fa=q.front();        q.pop();
        for(i=0;i<rod[fa].size();i++){
            int chl=rod[fa][i].to;
            if(dis[fa]==dis[chl]+rod[fa][i].val){
               nxt=rod[fa][i];
               mps[fa].push_back(nxt);
               nxt.to=fa;
               mps[chl].push_back(nxt);
               if(!vis[chl]) {    vis[chl]=1;  q.push(chl);   }
            }
        }
    }
}
int dfn[20009],low[20009],indx;
int belong[20009],typ;
bool instk[20009];
stack<int> stk;
void tarjan(int nw,int fa){
    int nxt,i,t=0;
    stk.push(nw);      instk[nw]=1;
    dfn[nw]=low[nw]= ++indx;
    for(i=0;i<mps[nw].size();i++){
        nxt=mps[nw][i].to;
        if(nxt==fa)    {
           t++;                    //记录若有多重边则不视为桥
           if(t<=1)  continue;
        }
        if(dfn[nxt]==-1){
           tarjan(nxt,nw);
           low[nw]=min(low[nw],low[nxt]);
        }
        else /*if(instk[nxt])*/{
           low[nw]= min(low[nw],dfn[nxt]);
        }
    }
    if(low[nw]==dfn[nw]){
       typ++;
       do{
           nxt=stk.top();      stk.pop();
           belong[nxt]=typ;
       }while(nxt!=nw);
    }
}

int main()
{
    #ifdef LOCAL
       freopen("E:\DATA.txt","r",stdin);
    #endif                       //*/
    int i=0,j=0;
    int a,b,k;
    //puts("OK------");
    while(scanf("%d %d",&n,&m)!=EOF)
    {
         //printf("n-    %lld %lld\n",n,m);

         for(i=1;i<=m;i++){
             scanf("%d %d %d",&a,&b,&k);
             node tmp;     tmp.id=i; tmp.val=k;
             tmp.to=b;     rod[a].push_back(tmp);
             tmp.to=a;     rod[b].push_back(tmp);
         }
         dijkstra();       bfs();           // 点1到点N的最短路

         clearto(dfn,-1);  clearto(low,-1);
         clearto(instk,0); indx=typ=0;
         tarjan(1,-1);

         set<int> ans;
         for(i=1;i<=n;i++){
             for(j=0;j<mps[i].size();j++){
                 if(belong[i]!=belong[mps[i][j].to])
                 {  ans.insert(mps[i][j].id);   }
             }
         }
         printf("%d\n",ans.size());
         set<int>::iterator it;
         for(it=ans.begin();it!=ans.end();it++)
         {   printf("%d ",*it);  }        printf("\n");

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值