#include <algorithm>
#include <bitset>
#include <cstdio>
#include <limits>
#include <list>
#include <queue>
#include <set>
#include <vector>
using ull = unsigned long long;
typedef std::list<int> NodeList;
typedef std::set<int> NodeSet;
struct Node
{
int index;
unsigned distance;
bool operator<(const Node& k) const {
return distance < k.distance;
}
};
struct Signal
{
int index;
unsigned volume;
};
unsigned mul(unsigned x, unsigned y) {
ull z = static_cast<ull>(x) * static_cast<ull>(y);
return std::min<ull>(z, std::numeric_limits<unsigned>::max());
}
int N, M;
std::set<int> W_forward[100];
std::set<int> W_reverse[100];
std::set<unsigned> amps[100][100];
std::set<unsigned> volumes[100];
std::vector<std::list<int>> cycles;
std::list<const std::list<int>*> cycle_pointers[100];
unsigned P[100];
unsigned D[100];
int policz() {
std::priority_queue<Node> kandydaci;
kandydaci.push({ N-1, 1 });
D[N-1] = 1;
std::fill(D, D+N-1, std::numeric_limits<unsigned>::max());
while (!kandydaci.empty()) {
Node k = kandydaci.top();
kandydaci.pop();
//printf("pętla dla #%d\n", k.index+1);
for (const int out : W_reverse[k.index]) {
//printf("próbujemy #%d #%d (%u)\n", k.index+1, w.out+1, w.amp);
unsigned new_d = mul(k.distance, *amps[out][k.index].begin());
if (new_d < D[out]) {
D[out] = new_d;
kandydaci.push({ out, new_d });
}
}
}
if (D[0] == std::numeric_limits<unsigned>::max()) {
return -1; // nie ma żadnej ścieżki od początku do końca
}
for (int i=0; i<N; ++i) {
if (D[i] != std::numeric_limits<unsigned>::max()) {
//printf("ruter #%d dostał odległość %u\n", i+1, D[i]);
P[i] = std::min(P[i], P[N-1] / D[i]);
//printf("nowa przepustowość to %u\n", P[i]);
}
}
bool changed = true;
while (changed) {
changed = false;
for (int i=N-2; i>=0; --i) {
unsigned max_out = 0;
for (const int out : W_forward[i]) {
max_out = std::max(max_out, P[out] / *amps[i][out].begin());
}
if (max_out < P[i]) {
P[i] = max_out;
changed = true;
}
}
}
if (P[0] == 0) {
return -1;
}
std::queue<Signal> queue;
volumes[0].insert(1);
queue.push({0, 1});
//fputs("START QUEUE", stderr);
while (!queue.empty()) {
Signal s = queue.front();
queue.pop();
//printf("#%d vol=%u\n", s.index+1, s.volume);
for (const int out : W_forward[s.index]) {
// można od razu określać max, a nie mnożyć po wszystkich
// ale wcale nie działa szybciej!
//unsigned max_amp = P[out] / s.volume;
//auto it_after = amps[s.index][out].upper_bound(max_amp);
for (unsigned amp : amps[s.index][out]) {
unsigned amped = mul(s.volume, amp);
if (amped <= P[out] && volumes[out].insert(amped).second) {
queue.push({out, amped});
}
}
}
}
//fputs("END QUEUE", stderr);
/*
for (int i=0; i<N; ++i) {
printf("VOLUMES #%d:\n", i+1);
for (unsigned v : volumes[i]) {
printf("%u\n", v);
}
}
*/
return volumes[N-1].empty() ? -1 : static_cast<int>(*volumes[N-1].rbegin());
}
int main() {
int T; scanf("%d", &T);
while (T-- > 0) {
scanf("%d%d", &N, &M);
for (int i=0; i<N; ++i) {
scanf("%u", &P[i]);
}
for (int j=0; j<M; ++j) {
int in, out;
unsigned amp;
scanf("%d%d%u", &in, &out, &);
if (in != out || amp > 1) {
W_forward[in-1].insert(out-1);
W_reverse[out-1].insert(in-1);
amps[in-1][out-1].insert(amp);
}
}
printf("%d\n", policz());
if (T > 0) {
for (int i=0; i<N; ++i) {
for (int j : W_forward[i]) {
amps[i][j].clear();
}
volumes[i].clear();
W_forward[i].clear();
W_reverse[i].clear();
cycle_pointers[i].clear();
}
cycles.clear();
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | #include <algorithm> #include <bitset> #include <cstdio> #include <limits> #include <list> #include <queue> #include <set> #include <vector> using ull = unsigned long long; typedef std::list<int> NodeList; typedef std::set<int> NodeSet; struct Node { int index; unsigned distance; bool operator<(const Node& k) const { return distance < k.distance; } }; struct Signal { int index; unsigned volume; }; unsigned mul(unsigned x, unsigned y) { ull z = static_cast<ull>(x) * static_cast<ull>(y); return std::min<ull>(z, std::numeric_limits<unsigned>::max()); } int N, M; std::set<int> W_forward[100]; std::set<int> W_reverse[100]; std::set<unsigned> amps[100][100]; std::set<unsigned> volumes[100]; std::vector<std::list<int>> cycles; std::list<const std::list<int>*> cycle_pointers[100]; unsigned P[100]; unsigned D[100]; int policz() { std::priority_queue<Node> kandydaci; kandydaci.push({ N-1, 1 }); D[N-1] = 1; std::fill(D, D+N-1, std::numeric_limits<unsigned>::max()); while (!kandydaci.empty()) { Node k = kandydaci.top(); kandydaci.pop(); //printf("pętla dla #%d\n", k.index+1); for (const int out : W_reverse[k.index]) { //printf("próbujemy #%d #%d (%u)\n", k.index+1, w.out+1, w.amp); unsigned new_d = mul(k.distance, *amps[out][k.index].begin()); if (new_d < D[out]) { D[out] = new_d; kandydaci.push({ out, new_d }); } } } if (D[0] == std::numeric_limits<unsigned>::max()) { return -1; // nie ma żadnej ścieżki od początku do końca } for (int i=0; i<N; ++i) { if (D[i] != std::numeric_limits<unsigned>::max()) { //printf("ruter #%d dostał odległość %u\n", i+1, D[i]); P[i] = std::min(P[i], P[N-1] / D[i]); //printf("nowa przepustowość to %u\n", P[i]); } } bool changed = true; while (changed) { changed = false; for (int i=N-2; i>=0; --i) { unsigned max_out = 0; for (const int out : W_forward[i]) { max_out = std::max(max_out, P[out] / *amps[i][out].begin()); } if (max_out < P[i]) { P[i] = max_out; changed = true; } } } if (P[0] == 0) { return -1; } std::queue<Signal> queue; volumes[0].insert(1); queue.push({0, 1}); //fputs("START QUEUE", stderr); while (!queue.empty()) { Signal s = queue.front(); queue.pop(); //printf("#%d vol=%u\n", s.index+1, s.volume); for (const int out : W_forward[s.index]) { // można od razu określać max, a nie mnożyć po wszystkich // ale wcale nie działa szybciej! //unsigned max_amp = P[out] / s.volume; //auto it_after = amps[s.index][out].upper_bound(max_amp); for (unsigned amp : amps[s.index][out]) { unsigned amped = mul(s.volume, amp); if (amped <= P[out] && volumes[out].insert(amped).second) { queue.push({out, amped}); } } } } //fputs("END QUEUE", stderr); /* for (int i=0; i<N; ++i) { printf("VOLUMES #%d:\n", i+1); for (unsigned v : volumes[i]) { printf("%u\n", v); } } */ return volumes[N-1].empty() ? -1 : static_cast<int>(*volumes[N-1].rbegin()); } int main() { int T; scanf("%d", &T); while (T-- > 0) { scanf("%d%d", &N, &M); for (int i=0; i<N; ++i) { scanf("%u", &P[i]); } for (int j=0; j<M; ++j) { int in, out; unsigned amp; scanf("%d%d%u", &in, &out, &); if (in != out || amp > 1) { W_forward[in-1].insert(out-1); W_reverse[out-1].insert(in-1); amps[in-1][out-1].insert(amp); } } printf("%d\n", policz()); if (T > 0) { for (int i=0; i<N; ++i) { for (int j : W_forward[i]) { amps[i][j].clear(); } volumes[i].clear(); W_forward[i].clear(); W_reverse[i].clear(); cycle_pointers[i].clear(); } cycles.clear(); } } } |
English