您的位置:首页 > 其它

Codeforces Round #372 (Div. 2)

2017-08-22 09:33 176 查看
A题,每次输入的时候记录,保留上一个的值,做差判断

#include<bits/stdc++.h>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const double g=10.0,eps=1e-7;
const int N=100000+10,maxn=100+10,inf=0x3f3f3f3f3f;

struct edge{
int from,to,Next,c;
}e[N*2];
int cnt,head
;
int d
;
int n,m,L,s,t;
bool vis
;
vector<int>p;
void add(int u,int v,int w)
{
e[cnt].to=v;
e[cnt].c=w;
e[cnt].Next=head[u];
head[u]=cnt++;
}
void spfa()
{
memset(vis,0,sizeof vis);
vis[s]=1;
memset(d,inf,sizeof d);
d[s]=0;
queue<int>q;
q.push(s);
while(!q.empty())
{
int x=q.front();
q.pop();
vis[x]=0;
for(int i=head[x];~i;i=e[i].Next)
{
int To=e[i].to,W=e[i].c;
if(d[To]>d[x]+W)
{
d[To]=d[x]+W;
if(!vis[To])
{
vis[To]=1;
q.push(To);
}
}
}
}
}
int getspfa(ll c)
{
//  cout<<c<<" ";
for(int i=0;i<p.size();i++)
{
e[p[i]].c=e[p[i]^1].c=1+min(c,(ll)1e9);
//   cout<<e[p[i]].c<<" ";
c-=e[p[i]].c-1;
}
//  cout<<endl;
spfa();
return d[t];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m>>L>>s>>t;
cnt=0;
memset(head,-1,sizeof head);
for(int i=0;i<2*m;i+=2)
{
cin>>e[i].from>>e[i].to>>e[i].c;
if(e[i].c==0)p.push_back(cnt);
add(e[i].from,e[i].to,e[i].c);
add(e[i].to,e[i].from,e[i].c);
}
if(getspfa(0)>L||getspfa(p.size()*(1e9))<L)
{
cout<<"NO"<<endl;
return 0;
}
ll l=0,r=p.size()*(1e9);
while(l<=r)
{
ll mid=(l+r)/2;
if(getspfa(mid)>L)r=mid-1;
else if(getspfa(mid)<L)l=mid+1;
else break;
}
cout<<"YES"<<endl;
for(int i=0;i<2*m;i+=2)
cout<<e[i].from<<" "<<e[i].to<<" "<<e[i].c<<endl;
return 0;
}
/********************
4 4 13 1 3
1 3 14
2 3 0
2 0 0
1 0 0
********************/


D
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: