题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4762
Time limit: 3000 ms

Problem Description

You are in charge of preparing the conference hall for the closing ceremony of ACM ICPC. You had hired a perfectionist sentimental room designer to design an elegant decoration for the old hall. The ceremony is going to start in a few hours and the designer has just informed you of the completion of the decoration. When you reach the conference hall, you confront a strange circuit on the wall. The designer starts explaining the meanings of life and ACM ICPC hidden under each piece of the circuit. The circuit consists of LEDs (Light Emitting Diodes) interconnected with junctions and wires. You ask whether the LEDs should be switched on. “Of course!” the designer responds, “Each and every LED must be lit, otherwise the whole work is junk!” You look around the circuit to find its power plug.

  • Where is the power plug?

  • Huh? It’s beyond the scope of my work! It is you who has to provide the electricity to the LEDs. And be careful! Do not remove or modify a single part of the circuit; Just connect power supply cables to the junctions. I have to leave right now. I will send a report of my perfect work to your manager. Bye.

Left alone with the bizarre circuit, you start inspecting the circuit.

Unlike incandescent light bulbs, which emit light regardless of the electrical polarity, LEDs only emit light with the correct electrical polarity (i.e. when their anode pin has a higher electric potential than the cathode pin). Each LED has a minimum voltage. An LED does not emit (even with the correct polarity) if the electrical potential difference of its pins is less than its minimum voltage. Each LED has also a maximum voltage. An LED is destroyed if its electrical potential difference exceeds its maximum voltage.

Your inspection on the circuit shows it consists of three types of components:

  • LEDs: Fortunately, all LEDs of the circuit are of the same type, and so having the same minimum voltage, and the same maximum voltage.
  • Junctions: Each of the two pins of an LED in the circuit is connected to a junction. Junctions are not only a place for connecting the LED pins, but also for connecting the wire end-points.
  • Wires: Each wire has two end-points and connects a junction directly to another junction forcing them to have the same electric potential.

By connecting external electrical poles (with different values of voltage) to each of the junctions of the circuit, you can inject different electric potentials to the junctions. Note that each junction must be connected to an external electrical pole. Be careful of short circuits: the end-points of each wire MUST have the same electric potential. By convention, we can assume the minimum electric potential to be zero. So, all the electric potentials can be considered to be nonnegative.

Now, you have to buy an external power supply that provides you with the required electrical poles. The cost of such power supplies depends on their upper-bound: the maximum voltage they provide.

Given the specification of the LED circuit, you have to write a program that tests if it is possible to light all the LEDs correctly (with no short circuits, and no LED destruction). In the case of the possibility, the program should also compute the minimum possible upper-bound of power supply with which all LEDs can emit light.

Input

The input consists of several test cases. Each test case describes an LED circuit and starts with a line containing 5 space-separated integers JJJ, LLL, WWW, mmm, and MMM, where JJJ is the number of junctions (2≤J≤500)(2 ≤ J ≤ 500)(2≤J≤500), LLL is the number of LEDs (1≤L≤5,000)(1 ≤ L ≤ 5, 000)(1≤L≤5,000), WWW is the number of wires (0≤W≤5,000)(0 ≤ W ≤ 5, 000)(0≤W≤5,000), and mmm and MMM are the minimum and maximum voltage of LEDs respectively (1≤m<M≤1000)(1 ≤ m < M ≤ 1000)(1≤m<M≤1000). Assume that the junctions are numbered 111 through JJJ.

Each of the next LLL lines represents an LED with two space-separated integers. The first integer is the number of the junction to which the anode pin of the LED is connected and the second integer is the number of the junction for the cathode pin. Then, WWW lines follow, each describing a wire of the circuit using two space-separated integers: the junctions the wire directly connects.

The input terminates with a line containing ‘00000’‘0 0 0 0 0’‘00000’ (omit the quotes).

Output

Write a single line of output for each test case. If there is no way to correctly light all the LEDs in the circuit of that test case, only write the word ‘Impossible’‘Impossible’‘Impossible’ (with no quotes). Otherwise, write a single integer: the minimum upperbound of power supply with which all the LEDs can be lit.

Sample Input

2 1 0 3 5
1 2
3 2 0 3 5
1 2
3 2
3 2 0 3 5
3 2
1 3
3 1 1 3 5
1 2
2 3
3 1 2 3 5
1 2
2 3
3 1
3 3 0 3 5
1 2
2 3
1 3
3 3 0 3 6
1 2
2 3
1 3
4 2 2 2 7
1 2
3 4
3 1
2 4
4 2 2 2 7
1 2
3 4
3 2
1 4
0 0 0 0 0

Sample Output

3
3
6
3
Impossible
Impossible
6
2
Impossible

Problem solving report:

Description:给出结点的数量、LED的数量以及结点的连接状态、导线数以及结点的连接状态、LED的最小和最大电压,求使其所有LED灯点亮的最小电压。
Problem solving:利用最短路的方法求,每个LED之间的电压正向为m,反向设为-M,而导线两端设为0,故我们只需要虚构一个结点0,求出0到其余结点的所有电压,其中最大和最小的差值即为满足条件的最小电压值。

Accepted Code:

/* * @Author: lzyws739307453 * @Language: C++ */
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 505;
const int MAXM = 1e4 + 5;
const int inf = 0x3f3f3f3f;
struct edge {int to, da, ne;edge() {}edge(int to, int da, int ne) : to(to), da(da), ne(ne) {}
}e[MAXM << 1];
int n, cnt;
bool vis[MAXN];
int dis[MAXN], inq[MAXN], he[MAXN];
void Add(int u, int v, int w) {e[++cnt] = edge(v, w, he[u]);he[u] = cnt;
}
bool Spfa() {queue <int> Q;for (int i = 1; i <= n; i++) {Q.push(i);inq[i] = 0;dis[i] = 0;vis[i] = true;}while (!Q.empty()) {int u = Q.front();Q.pop();vis[u] = false;for (int i = he[u]; ~i; i = e[i].ne) {int v = e[i].to;if (dis[v] < dis[u] + e[i].da) {dis[v] = dis[u] + e[i].da;if (++inq[v] >= n)return true;if (!vis[v]) {Q.push(v);vis[v] = true;}}}}return false;
}
int main() {int u, v, l, w, m, M;while (scanf("%d%d%d%d%d", &n, &l, &w, &m, &M), n) {cnt = 0;memset(he, -1, sizeof(he));for (int i = 0; i < l; i++) {scanf("%d%d", &u, &v);Add(u, v, m);Add(v, u, -M);}for (int i = 0; i < w; i++) {scanf("%d%d", &u, &v);Add(u, v, 0);Add(v, u, 0);}if (Spfa()) {printf("Impossible\n");continue;}int min_ = inf, max_ = -inf;for (int i = 1; i <= n; i++) {min_ = min(min_, dis[i]);max_ = max(max_, dis[i]);}printf("%d\n", max_ - min_);}return 0;
}

UVALive - LED Circuit(Spfa)相关推荐

  1. 西北工业大学计算机毕业论文,光纤通信发射机本科毕业论文 西北工业大学.docx...

    光纤通信发射机本科毕业论文 西北工业大学 本科毕业设计论文 PAGE 41 摘 要 直接调制光发射机能完成对电信号的处理,其功能是把电脉冲信号变成光脉冲信号.本文先介绍了光纤通信的发展背景,光纤相对于 ...

  2. UVALive 4223 Trucking 二分+spfa

    Trucking 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...

  3. 可否使用串联LED(或者光敏LED)来制作光电检测板?

    简 介: 在通常情况下,使用LED都是利用它的将电能转换成光的功能.它虽然有光敏特性,但比起光电池.光敏三极管效率低.但LED作为批量的器件,它的价格低廉.所以如果能够开发它作为光电传感器则会在很多应 ...

  4. raspberry pi_使用Raspberry Pi和GPIO引脚控制外部LED

    raspberry pi by Shahbaz Ahmed Shahbaz艾哈迈德(Shahbaz Ahmed) 使用Raspberry Pi和GPIO引脚控制外部LED (Controlling a ...

  5. HDU ACM 1224 Free DIY Tour (SPFA)

    Free DIY Tour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  6. 微型计算机系统与接口流水灯,单片机的LED流水灯系统设计2.doc

    单片机的LED流水灯系统设计2 学号:0809111030 2010 - 2011学年 第2学期 <单片机应用技术> 课 程 设 计 报 告 题 目: 单片机的LED流水灯系统设计 专 业 ...

  7. STEAM上的一款电路模拟神器 — CRUMB Circuit Simulator

    摘要:这几天在逛steam商店时,发现了一款有意思的电路仿真软件CRUMB Circuit Simulator(CRUMB电路模拟器),觉得挺有意思的,就下载了玩了一下. 这款模拟电路软件的东西不多, ...

  8. 【单片机应用】项目一 发光二极管LED控制

    发光二极管LED控制 一.LED介绍 二.LED的工作原理 三.小项目:点亮一个LED 点亮一个发光二极管 认识PROTEUS 用PROTEUS设计第一个LED控制电路 工作过程 LED点亮程序 点亮 ...

  9. .playground文件_部署可教学机器:Circuit Playground Express,Arduino,P5.js,TinyUSB

    .playground文件 什么是可教学机? (What is Teachable Machine?) Teachable Machine is a web-based tool that makes ...

最新文章

  1. Supervisor行为分析和实践
  2. 巧用watch命令执行循环操作,来解放我们的双手
  3. 矩阵的特征值、特征向量及其代码求解实现
  4. 如何设置input实现同时选中多个文件并同时上传
  5. html编辑器kindeditor我的使用方法 (转载)
  6. python json dict对象使用_Python中:dict(或对象)与json之间的互相转化
  7. 样式缓存没更新_差点没认出来:Office 2019/365桌面新图标来啦
  8. C++代理 Surrogate
  9. Python 计算总分数和平均分 - Python零基础入门教程
  10. 生活中的算法的实际举例_驾校学的技术,在实际生活中,你能运用自如吗?
  11. python里删除range里的数字_python中range函数与列表中删除元素
  12. CSS快速学习5:文本溢出和XHTML元素分类
  13. tomcat context 配置 项目部署
  14. python批量从pdf中转换图片保存
  15. 极光推送JPush使用Java SDK开发
  16. win10如何删除用户计算机账户,Win10系统如何利用命令删除用户账户?
  17. 我的梦想是成为一名计算机程序员英语怎么说,我梦想将来成为一名程序员英语作文...
  18. python 下载图片 写硬盘 慢_天啦噜!知道硬盘很慢,但没想到比 CPU Cache 慢 10000000 倍...
  19. 使用wxml2canvas将微信小程序页面转为图片
  20. 忽略Xgboost的Warning

热门文章

  1. android 仿阅读,发布一个练笔的 Android 阅读器,轻微仿91 Android 阅读器【后续将提供源码】...
  2. Windows上免费epub阅读器推荐
  3. python计算银行利息_awk 计算银行利息-shell
  4. PAT-BASIC1062——最简分数
  5. 手把手搭建springcloud微服务,使用Eureka
  6. python读docx文件_Python读写docx文件的方法
  7. 每天3.2亿人在刷抖音,而那些优秀的人却看这公众号的精华文章!
  8. 一小时刷完英语口语常用3000词汇(绿色护眼版)day8-词汇701-800
  9. 一分钟对我们的重要意义
  10. 树莓派连接RTL-SDR直接使用命令收听FM广播