UVALive - LED Circuit(Spfa)

522 篇文章 3 订阅
71 篇文章 0 订阅

题目链接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 J J J, L L L, W W W, m m m, and M M M, where J J J is the number of junctions ( 2 ≤ J ≤ 500 ) (2 ≤ J ≤ 500) (2J500), L L L is the number of LEDs ( 1 ≤ L ≤ 5 , 000 ) (1 ≤ L ≤ 5, 000) (1L5,000), W W W is the number of wires ( 0 ≤ W ≤ 5 , 000 ) (0 ≤ W ≤ 5, 000) (0W5,000), and m m m and M M M are the minimum and maximum voltage of LEDs respectively ( 1 ≤ m < M ≤ 1000 ) (1 ≤ m < M ≤ 1000) (1m<M1000). Assume that the junctions are numbered 1 1 1 through J J J.

Each of the next L L L 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, W W W 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 ‘ I m p o s s i b l e ’ ‘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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ityanger

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值