Codeforces 715B Complete The Graph(dijkstra+heap)
来源:互联网 发布:cms系统开发教程 编辑:程序博客网 时间:2024/05/22 06:42
代码:
#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <string>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <sstream>#include <cstdlib>#include <iostream>#include <algorithm>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;#define MAX 100005#define MAXN 6005#define maxnode 15#define sigma_size 30#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define lrt rt<<1#define rrt rt<<1|1#define middle int m=(r+l)>>1#define LL long long#define ull unsigned long long#define mem(x,v) memset(x,v,sizeof(x))#define lowbit(x) (x&-x)#define pii pair<int,int>#define bits(a) __builtin_popcount(a)#define mk make_pair#define limit 10000//const int prime = 999983;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f;const double pi = acos(-1.0);//const double inf = 1e18;const double eps = 1e-8;const LL mod = 1e9+7;const ull mx = 133333331;/*****************************************************/inline void RI(int &x) { char c; while((c=getchar())<'0' || c>'9'); x=c-'0'; while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0'; }/*****************************************************/struct Edge{ int u,v,next,c;}edge[MAX],e[MAX];int head[MAX];LL dis[MAX];int vis[MAX];int tot;int n;int L;void init(){ mem(head,-1); tot=0;}void add_edge(int a,int b,int c){ edge[tot]=(Edge){a,b,head[a],c}; head[a]=tot++;}LL spfa(int s,int t){ for(int i=0;i<n;i++) dis[i]=1e18; mem(vis,0); queue<int> q; q.push(s); dis[s]=0; vis[s]=1; while(!q.empty()){ int u=q.front();q.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v; if(dis[v]>dis[u]+edge[i].c){ dis[v]=dis[u]+edge[i].c; if(!vis[v]){ vis[v]=1; q.push(v); } } } } return dis[t];}struct Node{ int id,val; bool operator<(const Node &a)const{ return val>a.val; }}x;LL dijkstra(int s,int t){ priority_queue<Node> q; for(int i=0;i<n;i++) dis[i]=1e18; mem(vis,0); x=(Node){s,0}; dis[s]=0; q.push(x); while(!q.empty()){ x=q.top(); q.pop(); int u=x.id; if(vis[u]) continue; vis[u]=1; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v; if(!vis[v]&&dis[v]>dis[u]+edge[i].c){ dis[v]=dis[u]+edge[i].c; q.push((Node){v,dis[v]}); } } } return dis[t];}int main(){ int m,s,t; while(cin>>n>>m>>L>>s>>t){ init(); int num=0; for(int i=0;i<m;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); if(c){ add_edge(a,b,c); add_edge(b,a,c); } e[i]=(Edge){a,b,0,c}; } if(spfa(s,t)<L){ printf("NO\n"); continue; } for(int i=0;i<m;i++){ if(e[i].c) continue; add_edge(e[i].u,e[i].v,1); add_edge(e[i].v,e[i].u,1); e[i].c=1; LL tmp=dijkstra(s,t); if(tmp<L){ e[i].c=(L-(tmp-1)); edge[tot-1].c=e[i].c; edge[tot-2].c=e[i].c; } } if(dijkstra(s,t)>L){ printf("NO\n"); continue; } cout<<"YES"<<endl; for(int i=0;i<m;i++){ printf("%d %d %d\n",e[i].u,e[i].v,e[i].c); } } return 0;}
0 0
- Codeforces 715B Complete The Graph(dijkstra+heap)
- CodeForces 715B Complete The Graph(Dijkstra)
- codeforces 715B. Complete The Graph
- CodeForces 715B Complete The Graph
- Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
- Codeforces Round #372 (Div. 2) -- D. Complete The Graph(Dijkstra单源最短路)
- Codeforces 716D D. Complete The Graph
- Codeforces 716D D. Complete The Graph
- CodeForces 716D Complete The Graph (SPFA)
- Codeforces Round #372 Complete The Graph
- Codeforces Round #372 (Div. 1) B. Complete The Graph 解题报告
- CodeForces 716B Complete The Graph(二分搜索)(最短路)
- Codeforces Round #372 (Div. 2) D. Complete The Graph 图论、最短路、Dijkstra、路径、分配部分边权
- 【Dijkstra模板】codeforces715B Complete The Graph(最短路径)
- Codeforces 716B B. Complete the Word
- Dijkstra+Heap-HDU-4725-The Shortest Path in Nya Graph
- Codeforces-716B-Complete the Word
- Codeforces 716B Complete the Word
- 系统架构师考试笔记记录,持续补充中
- Mysql 启动慢查询日志 (不用重启)
- 最全的hadoop2.4.1版本分布式集群高可用模式安装步骤
- 状态压缩(3)+dp--hdu1565(能力题)
- android小技巧之---获取状态栏高度
- Codeforces 715B Complete The Graph(dijkstra+heap)
- LayoutInflater和MenuInflater用法
- JAVAScript中DOM与BOM的差异分析
- django执行自定义sql语句
- MWPhotoBrowser性能优化
- 常用工具~teamviewer~请检查您的互联网连接 一般情况下您会使用代理
- PHPexcel
- svn把老库进行分库操作
- AutoFac属性自动注入