ACdream 1415 Important Roads(最短路&缩点&找桥)
来源:互联网 发布:淘宝联盟还要人脸认证 编辑:程序博客网 时间:2024/06/15 02:56
Important Roads
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
Sample Input
6 71 2 12 3 12 5 31 3 23 5 12 4 15 6 2
Sample Output
25 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;}
- ACdream 1415 Important Roads(最短路&缩点&找桥)
- AcDream 1415 Important Roads 解题报告(最短路 + 线段树)
- ACdream 1415 最短路+桥
- ACdream 1415 最短路+桥的判断
- Gym - 100338C Important Roads 最短路+tarjan
- acdream 1415 最短路+边联通求桥
- Acdream 1415 最短路+双连通
- ACdream 1198 Transformers' Mission(最短路)
- ASC22 C Important Roads|| ACdream 1415 【两次spfa+无向图割边】
- POJ 1724 ROADS(bfs最短路)
- poj1724--ROADS(最短路变形)
- Poj 3411 Paid Roads(最短路)
- poj-ROADS(最短路变形)
- ACdream 1100 瑶瑶饿了(最短路+dp)
- POJ3114(缩点+最短路)
- hdu3313 最大流找关键点,或者最短路找关键点.
- POJ1724 ROADS最短路问题
- POJ 1724 ROADS 最短路
- ccCArray.cpp 文件
- sort list--LeetCode
- web服务器被配置为不列出此目录的内容——Internet信息服务管理器(IIS)
- android--日期时间控件(十二)
- 二分查找的迭代实现
- ACdream 1415 Important Roads(最短路&缩点&找桥)
- hbase集群安装和shell操作
- UI中常用的控件ImageView\Button
- 第六届蓝桥杯:黄金连分数
- Activity的四种启动模式
- Palindrome Number
- 权限对目录的重要性
- Win7系统自带的无线网络共享WIFI
- 力所能及之hibernate简单的增删查改