HC_ssm

4554

HC_ssm
4个月前
#include<iostream>
#include<cstring>
using namespace std;

const int N = 510, M = 10010;

int n, m, k;
int dist[N], backup[N];

struct Edge
{
int a, b, w;
} edges[M];

int bellman_ford()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
for (int i = 0; i < k; i ++ )
{
memcpy(backup, dist, sizeof dist);
for (int j = 0; j < m; j ++ )
{
int a = edges[j].a, b = edges[j].b, w = edges[j].w;
dist[b] = min(dist[b], backup[a] + w);
}
}
if (dist[n] > 0x3f3f3f3f / 2) return -1;
return dist[n];
}

int main()
{
cin >> n >> m >> k;

for (int i = 0; i < m; i ++ )
{
cin >> edges[i].a >> edges[i].b >> edges[i].w;

}

int t = bellman_ford();
if (t == -1) cout << "impossible" << endl;
else cout << t;
return 0;

}


HC_ssm
4个月前
#include <cstring>
#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

const int N = 160010;
typedef pair<int, int> PII;

int n, m;
int e[N], w[N], h[N], ne[N], idx;
int dist[N];
bool st[N];

void add (int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

int dijkstra()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
priority_queue<PII, vector<PII>, greater<PII>> q;
q.push({0, 1});

while (q.size())
{
auto t = q.top();
q.pop();
int distance = t.first, ver = t.second;

if (st[ver]) continue;
st[ver] = true;

for (int i = h[ver]; i != -1; i = ne[i])
{
int j = e[i];
if (dist[j] > distance + w[i])
{
dist[j] = distance + w[i];
q.push({dist[j], j});
}
// 如果dist[j] <= distance + w[i]说明存在一个 dist[j] 比当前的，利用最优点更新dist[j]的结果还要小；
// 因此该点j存在两个情况
// 1、已经放入st中，那么q.push在不在if语句中结果都一样，会在下次选择到的时候continue掉，push在if语句中会少执行一次；
// 2、还未放入st中，那么这个点之前被更新过一次，只要之前更新了就一定会放入q中，那么再讲j放入q中则多做了一次；
// 简而言之，每次只找到最优的点并只push能够利用最优点进行更新的点即可，若不能更新则不用管；

}
}

if (dist[n] == 0x3f3f3f3f) return -1;

return dist[n];

}

int main()
{
scanf("%d%d", &n, &m);

memset(h, -1, sizeof h);

while (m -- )
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
}

cout << dijkstra() << endl;

return 0;
}


HC_ssm
4个月前
#include<iostream>
#include<queue>
#include<cstring>
#include<unordered_map>
using namespace std;
const int N = 100010;
int h[N], e[N], ne[N], idx = 0;
int n, m;
int d[N];
bool st[N];
void add (int a, int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx ++ ;
}

int bfs()
{
queue<int> q;
q.push(1);
memset(d, -1, sizeof d);
d[1] = 0;
while(q.size())
{
auto t = q.front();
q.pop();
for(int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];

if(d[j] == -1)
{
d[j] = d[t] + 1;
q.push(j);
}
}
}

return d[n];
}

int main(){
cin >> n >> m;
int a, b;
memset(h, - 1, sizeof h);
for(int i = 0; i < m; i ++ )
{
cin >> a >> b;

}

cout << bfs() ;
return 0;
}


HC_ssm
4个月前
#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
using namespace std;

const int N = 100010;

int h[N], e[N], ne[N], d[N], idx;
int n, m;
bool st[N];

{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

int main()
{
memset(h, -1, sizeof h);
cin >> n >> m;
int a, b;
queue<int> q;
for (int i = 0; i < m; i ++ )
{
cin >> a >> b;
d[b] += 1;
}
vector<int> ans;
int cnt = 0;

for (int i = 1; i <= n; i ++ )
{
if (d[i] == 0)
{
cnt ++ ;
q.push(i);
ans.push_back(i);
}
}

// for (int i = 1; i <= n; i ++ ) cout << d[i] << ' ';
// cout << endl;

while (q.size())
{
auto t = q.front();
q.pop();

for (int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
d[j] -= 1;
// if (t == 2) cout << j << d[j] << ' ' << endl;
if (d[j] == 0)
{
// if (t == 2) cout << j;
ans.push_back(j);
q.push(j);
cnt ++ ;

}
}
}
// cout << cnt;
if (cnt == n)
{
for (int i = 0; i < n; i ++ ) cout << ans[i] << ' ' ;
cout << endl;
}
else cout << -1 << endl;
return 0;

}



HC_ssm
4个月前
#include<iostream>
#include<cstring>
using namespace std;

const int N = 100010, M = N * 2;

int h[N], e[M], ne[M], idx;
bool st[N];
int n, ans = N;

{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

int dfs(int u)
{
st[u] = true;
int sum = 1, res = 0;
for (int i = h[u]; i != -1; i = ne[i])
{
int j = e[i];
if (!st[j])
{
int s = dfs(j);
res = max(res, s);
sum += s;
}
}
res = max(res, n - sum);
ans = min(ans, res);
return sum;
}

int main()
{
cin >> n;
memset(h, -1, sizeof h);
int a, b;
for (int i = 0; i < n; i ++ )
{
cin >> a >> b;
}
dfs(1);
cout << ans << endl;
return 0;
}


HC_ssm
4个月前
#include <iostream>

using namespace std;

const int N = 100010, M = 1000010;

int n, m;
int ne[N];
char s[M], p[N];

int main()
{
cin >> n >> p + 1 >> m >> s + 1;

// for (int i = 2, j = 0; i <= n; i ++ )
// {
//     while (j && p[i] != p[j + 1]) j = ne[j];
//     if (p[i] == p[j + 1]) j ++ ;
//     ne[i] = j;
// }

// for (int i = 1, j = 0; i <= m; i ++ )
// {
//     while (j && s[i] != p[j + 1]) j = ne[j];
//     if (s[i] == p[j + 1]) j ++ ;
//     if (j == n)
//     {
//         printf("%d ", i - n);
//         j = ne[j];
//     }
// }

for (int i = 2, j = 0; i <= n; i ++ )
{
while (j && p[i] != p[j + 1]) j = ne[j];
if (p[i] == p[j + 1]) j ++ ;
ne[i] = j;
}

for (int i = 1, j = 0; i <= m; i ++ )
{
while (j && s[i] != p[j + 1]) j = ne[j];
if (s[i] == p[j + 1]) j ++ ;
if (j == n)
{
printf("%d ", i - n);
j = ne[j];
}
}

return 0;
}



HC_ssm
4个月前
#include <iostream>

using namespace std;

const int N = 100010;

int stk[N], tt;

int main()
{
int n;
cin >> n;
while (n -- )
{
int x;
scanf("%d", &x);
while (tt && stk[tt] >= x) tt -- ;
if (!tt) printf("-1 ");
else printf("%d ", stk[tt]);
stk[ ++ tt] = x;
}

return 0;
}


HC_ssm
4个月前
#include <iostream>

using namespace std;

const int N = 1000010;

int a[N], q[N];

int main()
{
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);

int hh = 0, tt = -1;
for (int i = 0; i < n; i ++ )
{
if (hh <= tt && i - k + 1 > q[hh]) hh ++ ;

while (hh <= tt && a[q[tt]] >= a[i]) tt -- ;
q[ ++ tt] = i;

if (i >= k - 1) printf("%d ", a[q[hh]]);
}

puts("");

hh = 0, tt = -1;
for (int i = 0; i < n; i ++ )
{
if (hh <= tt && i - k + 1 > q[hh]) hh ++ ;

while (hh <= tt && a[q[tt]] <= a[i]) tt -- ;
q[ ++ tt] = i;

if (i >= k - 1) printf("%d ", a[q[hh]]);
}

puts("");

return 0;
}



HC_ssm
5个月前
#include<iostream>
#include<algorithm>

const int N = 50010;

using namespace std;

int n;

struct R
{
int w, s;
bool operator <(const R &W)const
{
return w + s > W.w + W.s;
}
}r[N];

int main()
{
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> r[i].w >> r[i].s;
sort(r, r + n);
int res = -2e9, sum = 0;
for (int i = n - 1; i >= 0; i -- )
{
// cout << r[i].w << endl;
res = max(res, sum - r[i].s);
sum += r[i].w;
}
cout << res;
return 0;

}


HC_ssm
5个月前
#include<iostream>
#include<algorithm>
using namespace std;
int p[100010];
int main()
{
int n;
cin >> n;
for(int i = 0; i < n;  i ++ ) cin >> p[i];
sort(p, p + n);
int md = p[n/2];
int res = 0 ;
for(int i = 0; i < n; i ++ ) res += abs(p[i] - md);
cout << res;
return 0;
}