十年OI一场空,不开longlong见祖宗
inline int read()
{
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}#include <bits/stdc++.h>
using namespace std;
int n, m, sx, sy, ex, ey;
char mp[110][110];
int dist[110][110];
int d[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
struct nd
{
int x, y;
};
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> mp[i][j];
if (mp[i][j] == 'S')
{
sx = i, sy = j;
}
if (mp[i][j] == 'T')
{
ex = i, ey = j;
}
}
}
memset(dist, -1, sizeof dist);
dist[sx][sy] = 0;
queue <nd> q;
q.push({sx, sy});
while (!q.empty())
{
int x = q.front().x, y = q.front().y;
q.pop();
if (x == ex && y == ey)
{
cout << dist[x][y];
return 0;
}
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1000010], u, v;
class uqset
{
private:
struct node
{
int fa;
int sz;
int ma;
}dsu[1000010];
int cnt;
public:
uqset(int len, int a[])
{
for (int i = 1; i <= len; i++)
{
dsu[i].sz = 1;
dsu[i].fa = i;
dsu[i].ma = a[i];
}
cnt = len;
}
int getroot(int i)
{
if (dsu[i].fa == i)
{
return i;
}
int a = getroot(dsu[i].fa);
dsu[i].fa = a;
return a;
}
void unio(int a, int b)
{
int x = getroot(a);
int y = getroot(b);
if (x != y)
{
dsu[x].fa = y;
cnt--;
dsu[y].sz += dsu[x].sz;
dsu[y].ma = max(dsu[y].ma, dsu[x].ma);
}
}
bool is_in_one_set(int a, int b)
{
return getroot(a) == getroot(b);
}
int count()
{
return cnt;
}
int sum(int len)
{
long long cnt = 0;
for (int i = 1; i <= len; i++)
{
if (dsu[i].fa == i)
{
cnt += dsu[i].ma * dsu[i].sz;
}
}
return cnt;
}
};
int br(int mod)
{
return abs(rand() * rand() * rand() * rand() * rand() * rand() * rand() * rand()) % mod + 1;
}
int main()
{
srand((unsigned) time (NULL));
cout << "*";
string s1 = "self_study000.in";
string s2 = "self_study000.out";
for (int i = 1; i <= 100; i++)
{
s1[12]++;
if (s1[12] == '9'+1)
{
s1[12] = '0';
s1[11] ++;
}
if (s1[11] == '9'+1)
{
s1[11] = '0';
s1[10] ++;
}
s2[12]++;
if (s2[12] == '9'+1)
{
s2[12] = '0';
s2[11] ++;
}
if (s2[11] == '9'+1)
{
s2[11] = '0';
s2[10] ++;
}
ofstream ofs;
ofs.open(s1);
if (i <= 20)
{
n = br(2000), m = br(2000);
ofs << n << " " << m << "\n";
long long cnt = 0;
for (int i = 1; i <= n; i++)
{
a[i] = br(1e9);
ofs << a[i] << " ";
cnt += a[i];
}
uqset s(n, a);
for (int i = 1; i <= m; i++)
{
u = br(n), v = br(n);
ofs << u << " " << v << "\n";
s.unio(u, v);
}
ofs << s.sum(n) - cnt;
}
else if (i <= 24)
{
n = br(1000000), m = 0;
ofs << n << " " << m << "\n";
long long cnt = 0;
for (int i = 1; i <= n; i++)
{
a[i] = br(1e9);
ofs << a[i] << " ";
cnt += a[i];
}
uqset s(n, a);
for (int i = 1; i <= m; i++)
{
u = br(n), v = br(n);
ofs << u << " " << v << "\n";
s.unio(u, v);
}
ofs << s.sum(n) - cnt;
}
else if (i <= 28)
{
n = br(1000000), m = br(1000000);
ofs << n << " " << m << "\n";
long long cnt = 0;
int k = br(1e9);
for (int i = 1; i <= n; i++)
{
a[i] = k;
ofs << a[i] << " ";
cnt += a[i];
}
uqset s(n, a);
for (int i = 1; i <= m; i++)
{
u = br(n), v = br(n);
ofs << u << " " << v << "\n";
s.unio(u, v);
}
ofs << s.sum(n) - cnt;
}
else if (i <= 40)
{
n = br(1000000), m = br(1000000);
ofs << n << " " << m << "\n";
long long cnt = 0;
int k = br(1e9), j = br(1e9), ind = br(n);
for (int i = 1; i <= n; i++)
{
if (i == ind)
{
a[i] = j;
}
else
{
a[i] = k;
}
ofs << a[i] << " ";
cnt += a[i];
}
uqset s(n, a);
for (int i = 1; i <= m; i++)
{
u = br(n), v = br(n);
ofs << u << " " << v << "\n";
s.unio(u, v);
}
ofs << s.sum(n) - cnt;
}
else if (i <= 100)
{
n = br(1000000), m = br(1000000);
ofs << n << " " << m << "\n";
long long cnt = 0;
for (int i = 1; i <= n; i++)
{
a[i] = br(1e9);
ofs << a[i] << " ";
cnt += a[i];
}
uqset s(n, a);
for (int i = 1; i <= m; i++)
{
u = br(n), v = br(n);
ofs << u << " " << v << "\n";
s.unio(u, v);
}
ofs << s.sum(n) - cnt;
}
cout << "*";
ofs.close();
}
return 0;
}