#include
#include
#include
#include
#include
#include
using namespace std;
const long long MAXN = 100001;
vector adj[2 * MAXN];
long long disc[2 * MAXN], low[2 * MAXN], scc[2 * MAXN];
bool inStack[2 * MAXN];
stack stk;
long long timer, sccCount;
void tarjanDFS(long long u)
{
disc[u] = low[u] = ++timer;
stk.push(u);
inStack[u] = true;
for (long long v : adj[u])
{
if (disc[v] == -1)
{ // v is not visited
tarjanDFS(v);
low[u] = min(low[u], low[v]);
}
else if (inStack[v])
{ // v is in current SCC
low[u] = min(low[u], disc[v]);
}
}
// if u is root of SCC
if (low[u] == disc[u])
{
while (true)
{
long long v = stk.top();
stk.pop();
inStack[v] = false;
scc[v] = sccCount;
if (v == u)
break;
}
++sccCount;
}
}
long long negate(long long x)
{
return x % 2 ? x - 1 : x + 1;
}
int main()
{
ifstream fin("scandal.in");
ofstream fout("scandal.out");
long long N, M;
fin >> N >> M;
for (long long i = 0; i < M; ++i)
{
long long x, y, c;
fin >> x >> y >> c;
--x;
--y; // zero-index adjustment
long long true_x = 2 * x, true_y = 2 * y;
long long false_x = true_x + 1, false_y = true_y + 1;
// Original graph construction
if (c == 0)
{
adj[false_x].push_back(true_y);
adj[false_y].push_back(true_x);
}
else if (c == 1)
{
adj[false_x].push_back(false_y);
adj[true_y].push_back(true_x);
}
else if (c == 2)
{
adj[false_y].push_back(false_x);
adj[true_x].push_back(true_y);
}
else if (c == 3)
{
adj[true_x].push_back(false_y);
adj[true_y].push_back(false_x);
}
}
fill(disc, disc + 2 * N, -1);
fill(low, low + 2 * N, -1);
fill(inStack, inStack + 2 * N, false);
timer = sccCount = 0;
for (long long i = 0; i < 2 * N; ++i)
{
if (disc[i] == -1)
tarjanDFS(i);
}
bool possible = true;
vector<long long> solution;
for (long long i = 0; i < N; ++i)
{
if (scc[2 * i] == scc[2 * i + 1])
{
possible = false;
break;
}
if (scc[2 * i] < scc[2 * i + 1])
{ // Choosing the one which appears later in the component list
solution.push_back(i + 1);
}
}
if (possible)
{
fout << solution.size() << "\n";
for (long long person : solution)
{
fout << person << "\n";
}
}
else
{
fout << "0\n";
}
fout.close();
fin.close();
return 0;
}
scrie asta dupa modelul asta #include
#include
#include
#include
#include
#include <unordered_set>
using namespace std;
#define NMAX 100001
#define INF (1LL << 60)
struct Dijkstra_Res {
// vector pt distantele minime
vector dist;
// vector pentru parinti
vector parent;
// constructor
explicit Dijkstra_Res(int n)
: dist(n + 1, INF), parent(n + 1, -1) {}
};
class Drumuri {
public:
void solve() {
read();
print(get_res());
}
private:
int n, m;
// nodurile de start si destinatia
int x, y, z;
// folosesc un vector de perechi pentru a retine muchia si costul
vector<pair<int, long long>> adj[NMAX];
// functie pentru citirea datelor din fisier
void read() {
ifstream fin("drumuri.in");
fin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
long long cost;
fin >> a >> b >> cost;
adj[a].push_back({b, cost});
}
fin >> x >> y >> z;
fin.close();
}
// functie care returneaza rezultatul
long long get_res() {
// aplic dijkstra pornind din fiecare sursa
auto res_x = dijkstra(x);
auto res_y = dijkstra(y);
// daca raman distante infinite le fac -1
if (res_x.dist[z] == INF || res_y.dist[z] == INF) {
return -1;
}
// returnez costurile minim pentru fiecare sursa pana la destinatie adunate
return res_x.dist[z] + res_y.dist[z];
}
// functie pentru algoritmul dijkstra
Dijkstra_Res dijkstra(int start) {
Dijkstra_Res result(n);
// coada pt noduri vizitate
set<pair<long long, int>> pq;
// initializez cu 0 distanta de start si o adaug in coada
result.dist[start] = 0;
pq.insert({0, start});
while (!pq.empty()) {
// extrag nodul din coada
pair<long long, int> top = *pq.begin();
long long dist = top.first;
int node = top.second;
pq.erase(pq.begin());
// parcurg vecinii
for (size_t i = 0; i < adj[node].size(); ++i) {
int neighbor = adj[node][i].first;
long long weight = adj[node][i].second;
// calculez distanta daca trecem prin nodul asta
long long new_dist = dist + weight;
// verific daca distanta e mai mica
if (new_dist < result.dist[neighbor]) {
pq.erase({result.dist[neighbor], neighbor});
result.dist[neighbor] = new_dist;
result.parent[neighbor] = node;
pq.insert({new_dist, neighbor});
}
}
}
return result;
}
// functie care printeaza rezultatul
void print(long long result) {
ofstream fout("drumuri.out");
fout << result;
fout.close();
}
};
int main() {
auto *rez = new (nothrow) Drumuri();
rez->solve();
delete rez;
return 0;
}
cu print get rez etc