题目描述
“咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门……
妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小明被带到了t区,而自己在s区。
该市有m条大道连接n个区,一条大道将两个区相连接,每个大道有一个拥挤度。小明的妈妈虽然很着急,但是不愿意拥挤的人潮冲乱了她优雅的步伐。所以请你帮她规划一条从s至t的路线,使得经过道路的拥挤度最大值最小。
输入输出格式
输入格式:
第一行四个数字n,m,s,t。
接下来m行,每行三个数字,分别表示两个区和拥挤度。
(有可能两个区之间有多条大道相连。)
输出格式:
输出题目要求的拥挤度。
输入输出样例
说明
数据范围
30% n<=10
60% n<=100
100% n<=10000,m<=2n,拥挤度<=10000
题目保证1<=s,t<=n且s<>t,保证可以从s区出发到t区。
样例解释:
小明的妈妈要从1号点去3号点,最优路线为1->2->3。
考察算法:SPFA变型(我也不知道为啥洛谷上这题那么多标签。。。)
难度:普及/提高-
#include<algorithm> #include<cstdio> #include<cmath> #include<queue> #define MAXN 0x7fffffff #define M 40001 using namespace std; queue<int> q; int n, m, s, t; int tot; int dis[M], vis[M]; int to[M], head[M], net[M], cap[M]; void add(int u, int v, int w) { //邻接链表存图 to[++tot] = v; net[tot] = head[u]; head[u] = tot; cap[tot] = w; to[++tot] = u; net[tot] = head[v]; head[v] = tot; cap[tot] = w; } void spfa(int x) { //SPFA算法 for(int i = 1; i <= n; i++) vis[i] = 0, dis[i] = MAXN; dis[x] = 0; vis[x] = 1; q.push(x); while(!q.empty()) { int y = q.front(); q.pop(); vis[y] = 0; for(int i = head[y]; i; i = net[i]) { int t = to[i]; if(dis[t] > max(dis[y], cap[i])) { dis[t] = max(dis[y], cap[i]); if(!vis[t]) vis[t] = 1, q.push(t); } } } } int main() { scanf("%d%d%d%d", &n, &m, &s, &t); for(int i = 1; i <= m; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); add(a, b, c); } spfa(s); printf("%d", dis[t]); return 0; }