Codeforces 509E. Breaking Good SPFA
来源:互联网 发布:java求1到100的乘积 编辑:程序博客网 时间:2024/05/20 03:39
简单SPFA,看名字让我想到了Breaking Bad。。。。。
/* ***********************************************Author :CKbossCreated Time :2015年04月03日 星期五 09时08分24秒File Name :CF509D.cpp************************************************ */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>using namespace std;const int maxn=100100;struct Edge{int to,next,ok;}edge[maxn*2];int Adj[maxn],Size;void init(){memset(Adj,-1,sizeof(Adj)); Size=0;}void Add_Edge(int u,int v,int c){edge[Size].to=v;edge[Size].ok=c;edge[Size].next=Adj[u];Adj[u]=Size++;}int n,m;/********spfa**********/int dist[maxn],pre[maxn];int WE[maxn];bool inq[maxn];void spfa(){memset(dist,63,sizeof(dist));memset(inq,false,sizeof(inq));dist[1]=0;queue<int> q ;q.push(1); inq[1]=true;while(!q.empty()){int u=q.front(); q.pop();for(int i=Adj[u];~i;i=edge[i].next){int v=edge[i].to;if(dist[v]>dist[u]+1){dist[v]=dist[u]+1;pre[v]=u;WE[v]=WE[u]+edge[i].ok;if(inq[v]==false){inq[v]=true; q.push(v);}}else if(dist[v]==dist[u]+1){if(WE[v]<WE[u]+edge[i].ok){WE[v]=WE[u]+edge[i].ok;pre[v]=u;}if(inq[v]==false){inq[v]=true; q.push(v);}}}inq[u]=false;}}typedef pair<int,int> pII;typedef pair<pII,int> pIII;bool vis[maxn];vector<pIII> vp;void huishu(){int u=n;while(true){int v=pre[u];if(v==1&&u==1) break;vis[u]=vis[v]=true;u=v;}for(int u=1;u<=n;u++){for(int j=Adj[u];~j;j=edge[j].next){int v=edge[j].to;if(v>u) continue;if(vis[u]==true&&vis[v]==true){if(edge[j].ok==0){vp.push_back(make_pair(make_pair(u,v),1));}}else if(edge[j].ok==1){vp.push_back(make_pair(make_pair(u,v),0));}}}printf("%d\n",(int)vp.size());for(int i=0,sz=vp.size();i<sz;i++)printf("%d %d %d\n",vp[i].first.first,vp[i].first.second,vp[i].second);}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout);scanf("%d%d",&n,&m);init();for(int i=0;i<=n;i++) pre[i]=i;for(int i=0;i<m;i++){int u,v,c;scanf("%d%d%d",&u,&v,&c);Add_Edge(u,v,c); Add_Edge(v,u,c);}spfa();huishu(); return 0;}
0 0
- Codeforces 509E. Breaking Good SPFA
- Breaking Good - CodeForces 507E
- Codeforces 507E Breaking Good【最短路SPFA+Dp+记录路径】好题~~~
- 最短路Codeforces E. Breaking Good
- CodeForces 507E Breaking Good 最短路
- Codeforces Round #287 (Div. 2) 507E E. Breaking Good
- E. Breaking Good
- Codeforces Round #287 (Div. 2) E. Breaking Good
- Codeforces Round #287 (Div. 2) 507 E. Breaking Good
- codeforces 287DIV2 E - Breaking Good(最短路)
- CodeForces 507E Breaking Good(最短路)
- Codeforces Round #287 (Div. 2) E. Breaking Good 最短路
- CodeForces 507E Breaking Good(最短路)
- CodeForces P507E Breaking Good
- 【CF 507E】Breaking Good
- CF507E Breaking Good(spfa+dp)
- Codeforces Round #287 (Div. 2) E. Breaking Good(最短路、dp)
- Codeforces Round #287 (Div. 2) E. Breaking Good(最短路、dp)
- Android Init进程详解
- Android一键分享至社交平台
- android进程
- xml解析
- Sql根据子类ID查找父类ID
- Codeforces 509E. Breaking Good SPFA
- 关于bios中设置u盘启动
- 代码优化
- C语言 实现strstri不区分大小写查找字符串
- iOS利用Runtime自定义控制器POP手势动画
- 数据库 数据类型
- android后台服务service全解析(中)--IntentService与Notification前台通知
- 判断一个文件时CPP还是C
- ajax跨域调用问题处理