Codeforces Gym 100814 G It is all about wisdom 二分+最短路
来源:互联网 发布:银行存款怎样划算知乎 编辑:程序博客网 时间:2024/06/04 19:10
一个无向图,没有重边。所有路有一个花费,有一个最小智慧值,经过某条路径需要本人的智慧值大于等于所有路径的值智慧。问花费不超过k时,从1到n的最小智慧值是多少。
时限11s,放的这么开应该想到二分答案。
二分最大智慧值,每次用spfa寻找有没有这样一条路径。
#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <bitset>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=100005,inf=0x3f3f3f3f;const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L);int head[maxn],dist[maxn];int num,k,n,m;bool inque[maxn];struct Edge {int from,to,pre,dist,wis;};Edge edge[maxn*2];void addedge (int from,int to,int dist,int wis) {edge[num]=(Edge){from,to,head[from],dist,wis};head[from]=num++;edge[num]=(Edge){to,from,head[to],dist,wis};head[to]=num++;}bool find(int n,int maxd,int mwis) {queue<int> q;q.push(1);meminf(dist);mem0(inque);dist[1]=0;inque[1]=1;while (!q.empty()) {int now=q.front();q.pop();inque[now]=0;for (int i=head[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (dist[now]+edge[i].dist<dist[to]&&dist[now]+edge[i].dist<k&&edge[i].wis<=mwis) {dist[to]=dist[now]+edge[i].dist;if (to==n) return true;if (!inque[to]) {inque[to]=1;q.push(to);}}}}return false;}int solve(int n) {int l,r,mid,ans=-1;l=1;r=1e9;while (l<=r) {mid=(l+r)/2;if (find(n,k,mid)) {ans=mid;r=mid-1;} else l=mid+1;}return ans;}int main() {int cas;scanf("%d",&cas);while (cas--) {num=0;scanf("%d%d%d",&n,&m,&k);int i,j,x,y,w,d;memset(head,-1,sizeof(head));for (i=1;i<=m;i++) {scanf("%d%d%d%d",&x,&y,&d,&w);addedge(x,y,d,w);}int ans=solve(n);printf("%d\n",ans);}return 0;}
阅读全文
0 0
- Codeforces Gym 100814 G It is all about wisdom 二分+最短路
- codeforces [Gym-100814G]
- Codeforces gym 101149 L 最短路
- It is All About The Data
- 【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)
- 【最短路】NEERC15 F Froggy Ford (Codeforces GYM 100851)
- It is all about Using Text & Fonts in Flash
- Codeforces Gym 100741G Yet Another Median Task 二分乱搞
- All is about C!
- codeforces gym 100548G
- CodeForces Gym 100735G
- CodeForces Gym 100735G
- Codeforces-GYM-100923G
- Codeforces Gym-101617G
- poj1094Sorting It All Out 最短路前基础拓扑排序
- It’s All About Performance
- Codeforces 575G Run for beer 最短路
- Codeforces gym 101350G 数学
- 小知♂识 除法取模 逆元♂
- Spring Boot(一):HelloWorld
- bzoj2160 拉拉队排练 manacher+差分
- Android流行UI布局——底部导航(BottomNavigationView+ViewPager+Fragment)
- linux文件概览
- Codeforces Gym 100814 G It is all about wisdom 二分+最短路
- J2EE入门
- Redis的图形界面管理监控工具的安装及使用
- LinearLayout加滑动条
- POJ:2796 Feel Good
- 编程中遇见的异常解决方法
- 图像基础、OpenCV入门4——图像边缘算法
- HDU5781(概率DP)
- 扛住100亿次请求?我们来试一试