【题目链接】:http://codeforces.com/problemset/problem/716/D

【题意】



给你一张图;

这张图上有一些边的权值未知;

让你确定这些权值(改成一个正整数)

使得s到t的最短路恰好为L

【题解】



首先;

算出两个值

temp1->所有的未知边的权值都为1->算出s到t的最短路;

temp2->所有的未知边的权值都为INF->算出s到t的最短路;

则必须要有

temp1<=L<=temp2

否则无解;

明白这个之后;

为每一个未知的边都标号;

标号为1..totl;

然后;

二分有多少条未知边的权值边为1;

->mid

找到最小的,使得在mid条未知边的权值为1的时候;

s到t的最短路小于L;

则第mid条边必然在s->t的最短路上;

则把那第mid条边再加上s->t的最短路与L的差值

(前Mid-1条边权值还是1);

(因为1的边越多,s到t的最短路是单调不上升的,所以这么做是可行的)

(又因为是>L和< L的边界,所以那个mid一定是在最短路上的,且没有它最短路就会大于L)



【Number Of WA】



1



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1100;
const int M = 20000+100;
const int INF = 1e9+1; int fir[N],nex[M],en[M],w[M],lable[M];
int totm,totl,n,m,L,s,t;
LL dis[N];
bool exsit[N];
queue <int> dl; void add(int x,int y,int z,int flag)
{
nex[totm] = fir[x];
fir[x] = totm;
en[totm] = y;
w[totm] = z;
if (flag) lable[totm] = totl;
totm++;
} LL spfa(int pre)
{
rep1(i,1,n) dis[i] = -1;
exsit[s] = true;
dl.push(s);
dis[s] = 0;
while (!dl.empty())
{
int x = dl.front();
dl.pop();
exsit[x] = false;
for (int i = fir[x];i>=0;i = nex[i])
{
int y = en[i],cost = w[i];
if (lable[i] && lable[i]<=pre) cost = 1;
if (lable[i] && lable[i]>pre) cost = INF;
if (dis[y]==-1 || dis[y]>dis[x]+cost)
{
dis[y] = dis[x]+cost;
if (!exsit[y])
{
exsit[y] = true;
dl.push(y);
}
}
}
}
return dis[t];
} void out_graph(int key,int sp )
{
rep1(i,1,n)
{
for (int j = fir[i];j >= 0;j = nex[j])
{
if (j&1) continue;
cout <<i-1<<' '<<en[j]-1<<' ';
int cost = w[j];
if (lable[j])
{
if (lable[j]<key) cost = 1;
if (lable[j]==key) cost = sp;
if (lable[j]>key) cost = INF;
}
cout << cost << endl;
}
}
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
cin >> n >> m >> L >> s >> t;s++,t++;
rep1(i,1,n) fir[i] = -1;
rep1(i,1,m)
{
int x,y,z;
cin >> x >> y >> z;
x++,y++;
if (z==0) totl++;
add(x,y,z,z==0);
add(y,x,z,z==0);
}
LL temp1 = spfa(totl),temp2 = spfa(0);
if (temp1 <= L && L <= temp2)
{
cout << "YES" << endl;
int l = 0,r = totl,ans;
LL tl,ansl;
while (l <= r)
{
int mid = (l+r)>>1;
tl = spfa(mid);
if (tl<=L)
ans = mid,r = mid-1,ansl = tl;
else
l = mid+1;
}
out_graph(ans,L-ansl+1);
}
else
cout << "NO" << endl;
return 0;
}

【codeforces 716D】Complete The Graph的更多相关文章

  1. 【Codeforces 1009D】Relatively Prime Graph

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 1000以内就有非常多组互质的数了(超过1e5) 所以,直接暴力就行...很快就找完了 (另外一开始头n-1条边找1和2,3...n就好 [代 ...

  2. 【Codeforces 340D】Bubble Sort Graph

    [链接] 我是链接,点我呀:) [题意] 让你根据冒泡排序的规则 建立一张图 问你这张图的最大独立子集的大小 [题解] 考虑a[i]会和哪些点连边? 必然是在a[i]左边且比它大的数字以及在a[i]右 ...

  3. 【Azure Developer】使用Microsoft Graph API 批量创建用户,先后遇见的三个错误及解决办法

    问题描述 在先前的一篇博文中,介绍了如何使用Microsoft Graph API来创建Azure AD用户(博文参考:[Azure Developer]使用Microsoft Graph API 如 ...

  4. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  5. 【codeforces 792D】Paths in a Complete Binary Tree

    [题目链接]:http://codeforces.com/contest/792/problem/D [题意] 给你一棵满二叉树; 给你初始节点; 给你若干个往上走,左走,右走操作; 让你输出一系列操 ...

  6. 【codeforces 755E】PolandBall and White-Red graph

    [题目链接]:http://codeforces.com/contest/755/problem/E [题意] 给你n个节点; 让你在这些点之间接若干条边;构成原图(要求n个节点都联通) 然后分别求出 ...

  7. 【34.57%】【codeforces 557D】Vitaly and Cycle

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【30.36%】【codeforces 740D】Alyona and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. Android开发时经经常使用的LogUtil

    在开发过程中经经常使用到Log.我们常写的一种方式就是自己定义一个LogUtil工具类 private static boolean LOGV = true; private static boole ...

  2. 【整合篇】Activiti业务与流程的整合

    对于不管是Activtit还是jbpm来说,业务与流程的整合均类似.启动流程是绑定业务.流程与业务的整合放到动态代理中 [java] view plain copy print" style ...

  3. SQL SERVER读书笔记:JOIN

    nested loop join:适用于小数据集,有索引的情况.不占用内存,不用tempdb. merge join:大数据,要排序,多对多,用tempdb: hash join:对大数据集,少用户使 ...

  4. [SCOI 2003] 字符串折叠

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1090 [算法] 区间DP [代码] #include<bits/stdc++. ...

  5. H3C交换机DHCP&nbsp;Server配置的六个方面

    H3C交换机DHCP Server配置的六个方面 在交换机上面配置DHCP内容是司空见惯的了.那么这里我们就讲解一下H3C交换机DHCP Server配置内容.之后的文章中,我们还对针对其他方面的配置 ...

  6. C#中DBNull问题

    当数据库中一个字段不是必填项时,在往数据库中插入数据的时候往往会插入一个空字符串就草草了事了.在这里用DBNull可以解决这个问题 /// <summary> /// 插入数据 /// & ...

  7. Centos7下Docker的使用

    一.安装Docker 1.1.查看原有系统是否已经安装docker yum list installed | grep docker 1.2.如果有则不需要继续安装,想重新安装,先卸载 yum -y ...

  8. C++中内存分配、函数调用和返回值问题

    转载博客:http://blog.csdn.net/q_l_s/article/details/52176159(源地址找不到,就贴了这位大神的博客地址,他也是转载的,不过要是学习的话,他的博客很不错 ...

  9. 转载:SoapUI之接口数据传递

    SoapUI之接口数据传递(TestCase.TestSuite传递) SoapUI之接口数据传递(step传递) SoapUI+Groovy做接口自动化测试 SoapUI中使用Conditional ...

  10. 模拟试题C

    模拟试题C 一.单项选择题(2′*14 =28′) 1.双线性法向插值法(Phong Shading)的优点是( ) A)法向计算精确 B)高光域准确 C)对光源和视点没有限制 D)速度较快 2.用编 ...