gym100812G(想法题,最短路变形,好题)
来源:互联网 发布:淘宝店铺简介如何写 编辑:程序博客网 时间:2024/06/14 06:56
In my opinion, people are divided into two categories: some live in the future while others — in the past. Should it be explained what category did I belong? The mystery I hunted for a half of my life, finally was taking shape. Good Magician didn't know where Dragon hid. But he knew that bandit dens in some cities of the kingdom were controlled by people from the Dragon's gang. For each city he gave me a reliable information, whether Dragon controlled that city or not.
I was staring on the map of Fairy Kingdom trying to understand where to go now. There were n cities in the Kingdom, connected by mroads, with the length of the j-th road equal to wj. I guessed that Dragon hid in one of two cities controlled by his gang with the shortest path between them. First of all, the traffic of Blue Tea was the highest between them, and secondly, in case of emergency it was possible to move quickly from one of the cities to the other. All that remained was just to find such pair of cities.
The first line contains two integers separated by a space: n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of cities in Fairy Kingdom and the number of roads between them, correspondingly.
The second line contains n integers separated by spaces. On the i-th position the number 1 is written, if the i-th city is under control of Dragon's gang; otherwise the number 0 is written there.
The next m lines contain three integers each, separated by spaces: ai, bi, wi (1 ≤ ai < bi ≤ n, 1 ≤ wi ≤ 109) — the numbers of cities connected by a road and the length of this road. The cities are numbered from 1. Each pair of cities is presented at most once.
In the first line output a single integer — the length of the shortest path between cities where Dragon presumably hides.
In the second line output two integers separated by a space — the numbers of these cities.
If there are several possible answers, output any of them. If no two cities controlled by Dragon's people have a path between them, output «No luck at all» without quotes.
4 41 0 0 11 2 11 3 22 4 33 4 1
34 1
7 91 0 1 1 0 0 11 2 51 4 1002 3 52 5 42 6 43 7 1004 5 26 7 25 6 3
74 7
2 10 01 2 1
No luck at all
题意:
城市有两种属性(0/1),找最近的两个属性为1的城市,输出最短路长度与起点终点。
题解:
SPFA变形,单源最短路转化为,所有点到某个1之间的最短路,然后枚举中间边,找合法解。
解法:
SPFA,将所有1点同时初始化为0,同时进栈进行松弛,最终可求出每一点到最近的1的距离及最近的1是哪个点。然后遍历所有边,若一条边两边都是1,则路径合法;若两边都是0,且两边最近的1不同点,则路径合法;若一边0一边1,且两边最近的1不同点,则路径合法。最后将所有合法路径取最小值即为答案。
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define fi first#define se secondtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const ll inf=9e18;const ll mod=1000000007;const int maxn=1e5+100;int head[maxn];struct edge{ int from,to,next; ll w;}e[maxn*2]; //int tol=0;void add(int u,int v,ll w){ e[++tol].to=v,e[tol].from=u,e[tol].next=head[u],e[tol].w=w,head[u]=tol;}int a[maxn];ll d[maxn];int pre[maxn];int q[maxn],inq[maxn];int main(){ int n,m; scanf("%d%d",&n,&m); rep(i,1,n+1) scanf("%d",&a[i]); while(m--) { int u,v; ll w; scanf("%d%d%I64d",&u,&v,&w); add(u,v,w),add(v,u,w); } int front=0,rear=0; rep(i,1,n+1) d[i]=inf; rep(i,1,n+1) { if(a[i]) { q[rear++]=i,inq[i]=1,d[i]=0,pre[i]=i; } } while(front!=rear) { int u=q[front++]; if(front>=maxn) front=0; inq[u]=0; for(int i=head[u];i;i=e[i].next) { int v=e[i].to; ll w=e[i].w; if(d[u]+w<d[v]) { d[v]=d[u]+w; pre[v]=pre[u]; if(inq[v]) continue; q[rear++]=v; inq[v]=1; if(rear>=maxn) rear=0; } } } ll ans=inf; int u,v; for(int i=0;i<=tol;i+=2) { int u1=e[i].from,v1=e[i].to; if(a[u1]&&a[v1]) { if(e[i].w<ans) ans=e[i].w,u=u1,v=v1; } else if(a[u1]) { if(pre[v1]!=u1) { if(e[i].w+d[v1]<ans) { ans=e[i].w+d[v1]; u=u1,v=pre[v1]; } } } else if(a[v1]) { if(pre[u1]!=v1) { if(e[i].w+d[u1]<ans) { ans=e[i].w+d[u1]; u=pre[u1],v=v1; } } } else { if(pre[v1]!=pre[u1]) { if(e[i].w+d[v1]+d[u1]<ans) { ans=e[i].w+d[v1]+d[u1]; u=pre[u1],v=pre[v1]; } } } } if(ans>=inf) puts("No luck at all"); else { printf("%I64d\n%d %d\n",ans,u,v); } return 0;}
- gym100812G(想法题,最短路变形,好题)
- (POJ 1797)Heavy Transportation 最大生成树|最短路变形 (理解最短路核心思想好题)
- Codeforces Round #372 (Div. 1) D. Complete The Graph(最短路,想法题,好题)
- 1713: 参观城市 (最短路变形 记录城市号 好题)
- PAT 1087. All Roads Lead to Rome (最短路变形_好题)
- ZOJ 3781 最短路(想法好题目)
- hdu 1874 最短路 优先队列+dijstra 最短路 1004题 1003题的变形
- LA 4080 最短路好题
- Uva 11374 最短路 好题
- arc084_b (思维+最短路)(好题)
- 最短路变形_Poj_2253
- 最短路变形ural1934
- poj1797 最短路变形
- dij最短路 变形
- CF95C volleyball [最短路+想法]
- hdu 3499 最短路变形
- hdu_1596最短路小变形
- HDU 4360 最短路变形
- Linux上如何安装JDK
- 自定义组合控件和属性
- 网站统计功能的设计和实现
- 半年总结——思想的转变
- 集合B(哈希表模板题)
- gym100812G(想法题,最短路变形,好题)
- intellij idea 2016.03 version control 忽略文件夹
- Search in Rotated Sorted Array
- Spring AOP的几种实现方式
- BZOJ2752 [HAOI2012]高速公路(road)
- root登陆设置
- 职责链模式(23)
- ios 技术支持网址
- linux 服务器安全防攻击