Codeforces 715B Complete The Graph(dijkstra+heap)

nm00stL
n1000m10000wa
O(nmlogn)
0LNO
1LLdis[t]+1
LNO
滿cf


代碼:

#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章