Skip to content

Commit 8c46b8c

Browse files
Added brush-v and cycle finding code
1 parent 1b15328 commit 8c46b8c

File tree

5 files changed

+220
-5
lines changed

5 files changed

+220
-5
lines changed

Codeforces/1716B/1716B.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ int main() {
1212
int arr[n];
1313
for(int i=0; i<n; i++) arr[i]=i+1;
1414
cout << n << endl;
15-
for(int i=0; i<n; i++) {
16-
for(int j=0; j<n; j++) cout << arr[j] << " ";
17-
cout << endl;
18-
swap(arr[i], arr[i+1]);
19-
}
15+
for(int j=0; j<n; j++) cout << arr[j] << " ";
16+
cout << endl;
2017
}
2118
}

Lightoj/brush-v

57.9 KB
Binary file not shown.

Lightoj/brush-v.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
IN THE NAME OF ALLAH
3+
Author: Moinul Hossain
4+
Northern University Bangladesh
5+
*/
6+
7+
#include<bits/stdc++.h>
8+
using namespace std;
9+
10+
typedef long long ll;
11+
typedef vector<int> vi;
12+
typedef vector<ll> vl;
13+
typedef vector<vi> vvi;
14+
typedef vector<vl> vvl;
15+
typedef pair<int,int> pii;
16+
typedef pair<double, double> pdd;
17+
typedef pair<ll, ll> pll;
18+
typedef vector<pii> vii;
19+
typedef vector<pll> vll;
20+
typedef double dl;
21+
22+
#define PB push_back
23+
#define F first
24+
#define S second
25+
#define all(a) (a).begin(),(a).end()
26+
#define rall(a) (a).rbegin(),(a).rend()
27+
#define sz(x) (int)x.size()
28+
29+
const double PI = acos(-1);
30+
const double eps = 1e-9;
31+
const int inf = 2000000000;
32+
const ll infLL = 1000000000000000000;
33+
#define MOD 1000000007
34+
35+
#define mem(a,b) memset(a, b, sizeof(a) )
36+
#define sqr(a) ((a) * (a))
37+
38+
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
39+
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
40+
41+
ll gcd ( ll a, ll b ) { return __gcd ( a, b ); }
42+
ll lcm ( ll a, ll b ) { return a * ( b / gcd ( a, b ) ); }
43+
44+
int dx[] = { 0, 0, +1, -1, -1 +1, -1, +1 };
45+
int dy[] = { +1, -1, 0, 0, -1, +1, +1, -1 };
46+
47+
const int mx=112;
48+
ll dist[mx];
49+
vii adj[mx];
50+
51+
void dijkstra(int s, int n) {
52+
for(int i=0; i<=n; i++) dist[i]=infLL;
53+
priority_queue<pll, vll, greater<pll>> pq;
54+
dist[s]=0;
55+
pq.push({0, s});
56+
57+
while(!pq.empty()) {
58+
int u = pq.top().S;
59+
ll currD = pq.top().F;
60+
pq.pop();
61+
62+
if(dist[u] < currD) continue;
63+
64+
for(auto p : adj[u]) {
65+
int v = p.F;
66+
int w = p.S;
67+
if(currD + w < dist[v]) {
68+
dist[v] = currD + w;
69+
pq.push({dist[v], v});
70+
}
71+
}
72+
}
73+
}
74+
75+
int main() {
76+
optimize();
77+
int t;
78+
cin >> t;
79+
for(int tc=1; tc<=t; tc++) {
80+
int n,m;
81+
cin >> n >> m;
82+
for(int i=0; i<=n; i++) adj[i].clear();
83+
for(int i=1; i<=m; i++) {
84+
int u, v, w;
85+
cin >> u >> v >> w;
86+
adj[u].PB({ v, w });
87+
adj[v].PB({ u, w });
88+
}
89+
dijkstra(1, n);
90+
if(dist[n] == infLL) cout << "Case " << tc << ": Impossible\n";
91+
else cout << "Case " << tc << ": " << dist[n] << endl;
92+
}
93+
}

cses/cycle-finding

45.1 KB
Binary file not shown.

cses/cycle-finding.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
IN THE NAME OF ALLAH
3+
Author: Moinul Hossain
4+
Northern University Bangladesh
5+
*/
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
9+
10+
typedef long long ll;
11+
typedef vector<int> vi;
12+
typedef vector<ll> vl;
13+
typedef vector<vi> vvi;
14+
typedef vector<vl> vvl;
15+
typedef pair<int,int> pii;
16+
typedef pair<double, double> pdd;
17+
typedef pair<ll, ll> pll;
18+
typedef vector<pii> vii;
19+
typedef vector<pll> vll;
20+
typedef double dl;
21+
22+
#define endl '\n'
23+
#define PB push_back
24+
#define F first
25+
#define S second
26+
#define all(a) (a).begin(),(a).end()
27+
#define rall(a) (a).rbegin(),(a).rend()
28+
#define sz(x) (int)x.size()
29+
30+
const double PI = acos(-1);
31+
const double eps = 1e-9;
32+
const int inf = 2000000000;
33+
const ll infLL = 9000000000000000000;
34+
#define MOD 1000000007
35+
36+
#define mem(a,b) memset(a, b, sizeof(a) )
37+
#define sqr(a) ((a) * (a))
38+
39+
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
40+
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
41+
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
42+
//
43+
//debug
44+
template<typename F,typename S>ostream&operator<<(ostream&os,const pair<F,S>&p){return os<<"("<<p.first<<", "<<p.second<<")";}
45+
template<typename T>ostream&operator<<(ostream&os,const vector<T>&v){os<<"{";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"}";}
46+
template<typename T>ostream&operator<<(ostream&os,const set<T>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<",";os<<*it;}return os<<"]";}
47+
template<typename T>ostream&operator<<(ostream&os,const multiset<T>&v) {os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"]";}
48+
template<typename F,typename S>ostream&operator<<(ostream&os,const map<F,S>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<it->first<<" = "<<it->second;}return os<<"]";}
49+
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
50+
void faltu(){cerr << endl;}
51+
template<typename T>void faltu(T a[],int n){for(int i=0;i<n;++i)cerr<<a[i]<<' ';cerr<<endl;}
52+
template<typename T,typename...hello>void faltu(T arg,const hello&...rest){cerr<<arg<<' ';faltu(rest...);}
53+
//#else
54+
//#define dbg(args...)
55+
56+
ll gcd ( ll a, ll b ) { return __gcd ( a, b ); }
57+
ll lcm ( ll a, ll b ) { return a * ( b / gcd ( a, b ) ); }
58+
59+
60+
int dx[] = { 0, 0, +1, -1, -1 +1, -1, +1 };
61+
int dy[] = { +1, -1, 0, 0, -1, +1, +1, -1 };
62+
63+
const int mx=2512;
64+
65+
struct edge {
66+
int u, v, w;
67+
};
68+
69+
ll dist[mx];
70+
int par[mx];
71+
vector<edge> e;
72+
73+
vi bellmanFord(int s, int n, int m) {
74+
vi cycle;
75+
for(int i=1; i<=n; i++) dist[i]=infLL;
76+
dist[s]=0;
77+
int x=-1;
78+
for(int i=1; i<=n; i++) {
79+
x=-1;
80+
for(int j=0; j<m; j++) {
81+
int u = e[j].u;
82+
int v = e[j].v;
83+
int w = e[j].w;
84+
85+
if(dist[u] + w < dist[v]) {
86+
dist[v] = dist[u] + w;
87+
par[v] = u;
88+
x = v;
89+
}
90+
}
91+
}
92+
if(x==-1) return cycle;
93+
94+
for(int i=1; i<=n; i++) {
95+
x=par[x];
96+
}
97+
98+
int y = x;
99+
while(y != x || sz(cycle) == 0) {
100+
cycle.PB(y);
101+
y=par[y];
102+
}
103+
104+
cycle.PB(x);
105+
reverse(all(cycle));
106+
return cycle;
107+
}
108+
109+
int main() {
110+
optimize();
111+
int n, m;
112+
cin >> n >> m;
113+
for(int i=1; i<=m; i++) {
114+
int u, v, w;
115+
cin >> u >> v >> w;
116+
e.PB({ u, v, w });
117+
}
118+
vi ans = bellmanFord(1, n, m);
119+
if(sz(ans) == 0) cout << "NO\n";
120+
else {
121+
cout << "YES\n";
122+
for(auto u : ans) cout << u << " ";
123+
cout << endl;
124+
}
125+
}

0 commit comments

Comments
 (0)