#include
#include
#include
#include
using namespace std;
const int MAXN = 2e6 + 100;
typedef long long ll;
struct st{
int ID;
ll value;
friend bool operator < (st x, st y){
return x.value > y.value;
}
st(){}
st(int ID, ll value){
this->ID = ID;
this->value = value;
}
};
struct Edge{
int next;//next edge
int to;// next ver
ll value;
}edge[MAXN];
int cnt = 0;
int head[MAXN];
int vis[MAXN];
ll dis[MAXN];
void Add_Edge(int u, int v, ll value){
edge[cnt].value = value;
edge[cnt].next = head[u];
edge[cnt].to = v;
head[u] = cnt++;
}
void dijstra(int n, int s){
priority_queue<st> q;
st now;
dis[s] = 0;
now.ID = s;
now.value = dis[s];
q.push(now);
while(!q.empty()){
st u = q.top();
q.pop();
if(vis[u.ID]) continue;
vis[u.ID] = 1;
for(int i=head[u.ID];i != -1;i = edge[i].next){
if(!vis[edge[i].to] && dis[u.ID] + edge[i].value < dis[edge[i].to]){
dis[edge[i].to] = dis[u.ID] + edge[i].value;
now.ID = edge[i].to;
now.value = dis[edge[i].to];
q.push(now);
}
}
}
}
int main(){
int n, m, s, u, v;
ll w;
scanf("%d%d", &m, &n);
memset(head, -1, sizeof head);
for(int i=0;i<=n;i++) dis[i] = 2147483647;
for(int i=0;i<m;i++){
scanf("%d%d%lld", &u, &v, &w);
Add_Edge(u, v, w);
Add_Edge(v, u, w);
}
dijstra(n, 1);
printf("%lld", dis[n]);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6 + 100;
const double eps = 1e-6;
int Data[MAXN];
struct NODE{
int x, y;
}node[MAXN];
struct Edge{
int to;
int next;
double val;
}edge[MAXN];
struct st{
int ID;
double value;
friend bool operator<(st x, st y){
return x.value > y.value;
}
};
int cnt = 0;
int head[MAXN];
int vis[MAXN];
int path[MAXN];
double dis[MAXN];
void Add_Edge(int u, int v, double w){
edge[cnt].next = head[u];
edge[cnt].to = v;
edge[cnt].val = w;
head[u] = cnt++;
}
void dijstra(int s){
priority_queue<st> q;
st now;
dis[s] = 0;
now.ID = s;
now.value = dis[s];
q.push(now);
while(!q.empty()){
st u = q.top();
q.pop();
if(vis[u.ID]) continue;
vis[u.ID] = 1;
for(int i=head[u.ID];i != -1;i=edge[i].next){
if(max(dis[u.ID], edge[i].val) < dis[edge[i].to] && !vis[edge[i].to]){
dis[edge[i].to] = max(edge[i].val, dis[u.ID]);
path[u.ID] = edge[i].to;
now.ID = edge[i].to;
now.value = dis[edge[i].to];
q.push(now);
}
}
}
}
int main(){
int n, x, y;
int num = 0;
while(cin >> n && n){
for(int i=0;i<n;i++){
cin >> node[i].x >> node[i].y;
}
memset(head, -1, sizeof head);
memset(vis, 0, sizeof vis);
memset(path, -1, sizeof path);
for(int i=0;i<=n;i++){
dis[i] = 999999999;
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
double DIS = sqrt(1.0 * (node[i].x - node[j].x) * (node[i].x - node[j].x) + 1.0 * (node[i].y - node[j].y) * (node[i].y - node[j].y));
Add_Edge(i, j, DIS);
Add_Edge(j, i, DIS);
}
}
dijstra(0);
int p = 0;
printf("Scenario #%d\n", ++num);
printf("Frog Distance = %.3f\n\n", dis[1]);
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e3 + 100;
const double eps = 1e-6;
struct Edge{
int next;
int to;
int val;
}edge[200000];
struct st{
int ID;
int val;
bool operator<(st x)const{
return val < x.val;
}
};
int cnt = 0;
int head[MAXN];
int dis[MAXN];
int vis[MAXN];
void Add_Edge(int u, int v, int w){
edge[cnt].next = head[u];
edge[cnt].to = v;
edge[cnt].val = w;
head[u] = cnt++;
}
void dijstra(int s){
priority_queue<st> q;
st now;
dis[s] = INF;
now.ID = s;
now.val = dis[s];
q.push(now);
while(!q.empty()){
st u = q.top();
q.pop();
if(vis[u.ID]) continue;
vis[u.ID] = 1;
for(int i=head[u.ID];i != -1;i=edge[i].next){
if(min(dis[u.ID], edge[i].val) > dis[edge[i].to] && ! vis[edge[i].to]){
dis[edge[i].to] = min(edge[i].val, dis[u.ID]);
now.ID = edge[i].to;
now.val = dis[edge[i].to];
q.push(now);
}
}
}
}
int main(){
int t, n, m, u, v, w;
scanf("%d", &t);
for(int i=1;i<=t;i++){
scanf("%d%d", &n, &m);
memset(vis, 0, sizeof vis);
memset(dis, -0x3f, sizeof dis);
memset(head, -1, sizeof head);
cnt = 0;
while(m--){
scanf("%d%d%d", &u, &v, &w);
Add_Edge(u, v, w);
Add_Edge(v, u, w);
}
dijstra(1);
printf("Scenario #%d:\n", i);
printf("%d\n\n", dis[n]);
}
return 0;
}
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAXN = 1e3 + 100;
struct st{
int ID;
int val;
bool operator<(st x)const {
return val > x.val;
}
};
struct Edge{
int next;
int to;
int val;
}edge[200000];
int head[MAXN];
int vis[MAXN];
int dis[MAXN];
int cnt;
void Add_Edge(int u, int v, int w){
edge[cnt].next = head[u];
edge[cnt].to = v;
edge[cnt].val = w;
head[u] = cnt++;
}
map<int, int> mp;
void dijstra(int s){
priority_queue<st> q;
st now;
dis[s] = 0;
now.ID = s;
now.val = dis[s];
q.push(now);
while(!q.empty()){
st u = q.top();
q.pop();
if(vis[u.ID]) continue;
vis[u.ID] = 1;
for(int i=head[u.ID];i!=-1;i=edge[i].next){
if(!vis[edge[i].to] && dis[u.ID] + edge[i].val < dis[edge[i].to]){
dis[edge[i].to] = dis[u.ID] + edge[i].val;
now.ID = edge[i].to;
now.val = dis[edge[i].to];
q.push(now);
}
}
}
}
int main(){
int n, m, x, u, v, w;
scanf("%d%d%d", &n, &m, &x);
memset(head, -1, sizeof head);
memset(dis, 0x3f, sizeof dis);
while(m--){
scanf("%d%d%d", &u, &v, &w);
Add_Edge(u, v, w);
}
dijstra(x);
for(int i=1;i<=n;i++){
mp[i] = dis[i];
}
int ans = -0x3f3f3f3f;
for(int i=1;i<=n;i++){
if(i == x) continue;
memset(dis, 0x3f, sizeof dis);
memset(vis, 0, sizeof vis);
dijstra(i);
ans = max(ans, mp[i] + dis[x]);
}
cout << ans;
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6 + 100;
const double eps = 1e-6;
int Data[MAXN];
double edge1[200][200];
double edge2[200][200];
double dis[200];
bool Bellman_Ford(int n){
double d[200];
for(int i=1;i<=n;i++){
d[i] = dis[i];
}
for(int k=1;k<n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(dis[j] < (dis[i] - edge2[i][j]) * edge1[i][j]){
dis[j] = (dis[i] - edge2[i][j]) * edge1[i][j];
}
}
}
}
for(int i=1; i<=n; i++){
if(d[i] < dis[i]) return true;
}
return false;
}
int main(){
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n, m, s, u, x, y;
double v;
double rab, rba, cab, cba;
scanf("%d%d%d%lf", &n, &m, &s, &v);
dis[s] = v;
while(m--){
scanf("%d%d%lf%lf%lf%lf", &x, &y, &rab, &cab, &rba, &cba);
edge1[x][y] = rab;
edge1[y][x] = rba;
edge2[x][y] = cab;
edge2[y][x] = cba;
}
Bellman_Ford(n);
if(Bellman_Ford(n)) printf("YES");
else printf("NO");
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6 + 100;
const double eps = 1e-6;
int Data[MAXN];
int edge[600][600];
void Floyd(int n){
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
if(edge[i][k] != INF)
for(int j=1;j<=n;j++){
if(edge[i][j] > edge[i][k] + edge[k][j]){
edge[i][j] = edge[i][k] + edge[k][j];
}
}
}
}
}
bool check(int n){
for(int i=1;i<=n;i++){
if(edge[i][i] < 0) return true;
}
return false;
}
int main(){
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int f, n, m, w, s, e, t;
cin >> f;
while(f--){
scanf("%d%d%d", &n, &m, &w);
memset(edge, 0x3f, sizeof edge);
for(int i=0;i<=n;i++){
edge[i][i] = 0;
}
for(int i=0;i<m;i++){
scanf("%d%d%d", &s, &e, &t);
if(edge[s][e] > t){
edge[s][e] = edge[e][s] = t;
}
}
for(int i=0;i<w;i++){
cin >> s >> e >> t;
edge[s][e] = min(edge[s][e], -t);
}
Floyd(n);
if(check(n)) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int N = 105;
int edge[N][N], dis[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
memset(edge, 0x3f, sizeof edge);
for(int i=2;i<=n;i++){
for(int j=1;j<i;j++){
string s;
cin >> s;
if(s == "x") continue;
int sz = s.length();
int now = 0;
for(int k=0;k<sz;k++){
now *= 10;
now += s[k] - '0';
}
edge[i][j] = edge[j][i] = now;
}
}
memset(dis, 0x3f, sizeof dis);
dis[1] = 0;
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dis[j] = min(dis[j], dis[i] + edge[i][j]);
}
}
}
int ans = 0;
for(int j=1;j<=n;j++){
ans = max(ans, dis[j]);
}
cout << ans;
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int N = 105;
const int INF = 0x3f3f3f3f;
int edge[N][N];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
memset(edge, 0x3f, sizeof edge);
for(int i=0;i<m;i++){
int u, v;
cin >> u >> v;
edge[u][v] = 1;
edge[v][u] = -1;
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(edge[i][k] == edge[k][j] && edge[i][k] != INF){
edge[i][j] = edge[i][k];
}
}
}
}
int ans = 0;
for(int i=1;i<=n;i++){
int num = 0;
for(int j=1;j<=n;j++){
if(edge[i][j] != INF) num += 1;
}
if(num == n - 1) ans += 1;
}
cout << ans;
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
int kase = 1;
while(cin >> n && n){
map<string, int> mp;
int cnt = 0;
for(int i=0;i<n;i++){
string s;
cin >> s;
mp[s] = ++cnt;
}
int m;
cin >> m;
vector<vector<double> > g(cnt + 1, vector<double>(cnt + 1));
bool ok = false;
for(int i=1;i<=cnt;i++) g[i][i] = 1;
for(int i=1;i<=m;i++){
string s1, s2;
double x;
cin >> s1 >> x >> s2;
g[mp[s1]][mp[s2]] = x;
}
for(int k=1;k<=cnt;k++){
for(int i=1;i<=cnt;i++){
for(int j=1;j<=cnt;j++){
g[i][j] = max(g[i][j], g[i][k] * g[k][j]);
}
}
}
for(int i=1;i<=cnt;i++) if(g[i][i] > 1) ok = true;
cout << "Case " << kase << ": " << (ok ? "Yes" : "No") << '\n';
kase += 1;
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
int n, m;
struct Edge{
int next;
int to;
int val;
}edge1[N << 1], edge2[N << 1];
int head1[N], head2[N], dis1[N], dis2[N];
bool vis1[N], vis2[N];
int cnt1, cnt2;
void Add_Edge(Edge edge[], int head[], int u, int v, int w, int& cnt){
edge[cnt].next = head[u];
edge[cnt].to = v;
edge[cnt].val = w;
head[u] = cnt++;
}
struct st{
int id;
int val;
st(){}
st(int id, int val): id(id), val(val){}
bool operator < (const st &B)const{
return val > B.val;
}
};
void dijkstra(int s, int dis[], Edge edge[], int head[], bool vis[]){
dis[s] = 0;
priority_queue<st> q;
q.push(st(s, dis[s]));
while(!q.empty()){
st u = q.top();
q.pop();
if(vis[u.id]) continue;
vis[u.id] = true;
for(int i=head[u.id];~i;i=edge[i].next){
int v = edge[i].to;
if(!vis[v] && u.val + edge[i].val < dis[v]){
dis[v] = u.val + edge[i].val;
q.push(st(v, dis[v]));
}
}
}
}
int main(){
int t;
scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &m);
cnt1 = cnt2 = 0;
memset(head1, -1, sizeof head1);
memset(head2, -1, sizeof head2);
memset(dis1, 0x3f, sizeof dis1);
memset(dis2, 0x3f, sizeof dis2);
memset(vis1, 0, sizeof vis1);
memset(vis2, 0, sizeof vis2);
for(int i=0;i<m;i++){
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
Add_Edge(edge1, head1, u, v, w, cnt1);
Add_Edge(edge2, head2, v, u, w, cnt2);
}
priority_queue<st> q;
dijkstra(1, dis1, edge1, head1, vis1);
dijkstra(1, dis2, edge2, head2, vis2);
ll ans = 0;
for(int i=1;i<=n;i++){
ans += dis1[i];
}
for(int i=1;i<=n;i++){
ans += dis2[i];
}
printf("%lld\n", ans);
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n, m;
struct Edge{
int next;
int to;
int val;
}edge[N << 1];
int head[N];
int vis[N];
int cnt;
void Add_Edge(int u, int v, int w){
edge[cnt].next = head[u];
edge[cnt].to = v;
edge[cnt].val = w;
head[u] = cnt++;
}
struct st{
int id;
int val;
st(){}
st(int id, int val): id(id), val(val){}
bool operator < (const st &B)const{
return val > B.val;
}
};
int dis[N];
void dijkstra(int s){
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;
priority_queue<st> q;
q.push(st(s, dis[s]));
while(!q.empty()){
st u = q.top();
q.pop();
if(vis[u.id]) continue;
vis[u.id] = true;
for(int i=head[u.id];~i;i=edge[i].next){
int v = edge[i].to;
if(!vis[v] && u.val + edge[i].val < dis[v]){
dis[v] = u.val + edge[i].val;
q.push(st(v, dis[v]));
}
}
}
}
int main(){
int n, m;
scanf("%d%d", &n, &m);
memset(head, -1, sizeof head);
for(int i=1;i<=m;i++){
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
Add_Edge(u, v, w);
}
dijkstra(1);
printf("%d", dis[n]);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int N = 202;
double edge[N][N];
struct st{
int id;
int x;
int y;
st(){}
st(int id, int x, int y): id(id), x(x), y(y){}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int x, y;
vector<st> vs, v;
v.push_back(st(0, x1, y1));
int cnt = 0;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
edge[i][j] = 1e18;
}
}
while(cin >> x >> y){
if(x == -1 && y == -1){
for(int i=0;i<(int)vs.size();i++){
edge[0][vs[i].id] = hypot(vs[i].x - x1, vs[i].y - y1) * 3.0 / 500;
edge[vs[i].id][0] = edge[0][vs[i].id];
edge[201][vs[i].id] = hypot(vs[i].x - x2, vs[i].y - y2) * 3.0 / 500;
edge[vs[i].id][201] = edge[201][vs[i].id];
}
for(int i=1;i<(int)vs.size();i++){
edge[vs[i - 1].id][vs[i].id] = hypot(vs[i].x - vs[i - 1].x, vs[i].y - vs[i - 1].y) * 3.0 / 2000;
edge[vs[i].id][vs[i - 1].id] = edge[vs[i - 1].id][vs[i].id];
}
vs.clear();
continue;
}
cnt += 1;
v.push_back(st(cnt, x, y));
vs.push_back(st(cnt, x, y));
}
v.push_back(st(201, x2, y2));
for(int i=0;i<(int)v.size();i++){
for(int j=0;j<(int)v.size();j++){
edge[v[i].id][v[j].id] = min(edge[v[i].id][v[j].id], hypot(v[i].x - v[j].x, v[i].y - v[j].y) * 3.0 / 500);
edge[v[j].id][v[i].id] = edge[v[i].id][v[j].id];
}
}
for(int k=0;k<N;k++){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
edge[i][j] = min(edge[i][j], edge[i][k] + edge[k][j]);
}
}
}
cout << (int)(edge[0][N - 1] + 0.5);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int N = 200;
struct st{
int p, l, x;
};
pair<int, int> mp[N][N];
const int INF = 0x3f3f3f3f;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> m >> n;
vector<st> s(n + 1);
vector<vector<pair<int, int> > > vs(n + 1);
vector<vector<pair<int, int> > > dis(n + 1, vector<pair<int, int> >(n + 1));// [路径上最小的地位等级, 最大的地位等级]
vector<vector<int> > g(n + 1, vector<int>(n + 1));
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dis[i][j].first = 0;
dis[i][j].second = n + 1;
g[i][j] = INF;
}
g[i][i] = 0;
}
for(int i=1;i<=n;i++){
cin >> s[i].p >> s[i].l >> s[i].x;
dis[i][i].first = dis[i][i].second = s[i].l;
for(int j=1;j<=s[i].x;j++){
int u, v;
cin >> u >> v;
g[i][u] = v;
vs[i].push_back(make_pair(u, v));
}
}
for(int i=1;i<=n;i++){
for(int j=0;j<(int)vs[i].size();j++){
if(abs(s[i].l - s[vs[i][j].first].l) <= m){
dis[i][vs[i][j].first].first = min(s[i].l, s[vs[i][j].first].l);
dis[i][vs[i][j].first].second = max(s[i].l, s[vs[i][j].first].l);
}else{
g[i][vs[i][j].first] = INF;
}
}
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(abs(dis[i][k].first - dis[k][j].second) > m || abs(dis[i][k].second - dis[k][j].first) > m) continue;
dis[i][j].first = min(dis[i][k].first, dis[k][j].first);
dis[i][j].second = max(dis[i][k].second, dis[k][j].second);
g[i][j] = min(g[i][k] + g[k][j], g[i][j]);
}
}
}
int ans = INF;
for(int i=1;i<=n;i++){
ans = min(ans, g[1][i] + s[i].p);
}
cout << ans;
return 0;
}
In every intersection there is a switch pointing to the one of the rails going out of the intersection. When the tram enters the intersection it can leave only in the direction the switch is pointing. If the driver wants to go some other way, he/she has to manually change the switch.
每个十字路口都有一个道岔,指向驶出十字路口的一条铁轨。当有轨电车进入十字路口时,它只能沿着道岔指向的方向离开。如果司机想换路,他/她必须手动改变开关。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, a, b;
cin >> n >> a >> b;
vector<vector<int> > g(n + 1, vector<int> (n + 1, INF));
for(int i=1;i<=n;i++){
int k;
cin >> k;
for(int j=0;j<k;j++){
int v;
cin >> v;
if(j == 0) g[i][v] = 0;
else g[i][v] = 1;
}
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
g[i][j] = min(g[i][k] + g[k][j], g[i][j]);
}
}
}
cout << (g[a][b] == INF ? -1 : g[a][b]);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int N = 300;
int a[N];
int dis[N];
int upd[N];
const int INF = 0x3f3f3f3f;
struct Edge{
int next;
int to;
int val;
}edge[N * N];
int head[N];
int cnt;
void Add_Edge(int u, int v, int w){
edge[cnt].next = head[u];
edge[cnt].to = v;
edge[cnt].val = w;
head[u] = cnt++;
}
int vis[N];
int f[N];
void Dfs(int u){
for(int i=head[u];~i;i=edge[i].next){
int v = edge[i].to;
if(f[v]) continue;
f[v] = 1;
Dfs(v);
}
}
int spfa(int s, int n){
queue<int> q;
dis[s] = 0;
vis[s] = 1;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = 0;
if(f[u]) continue;
for(int i=head[u];~i;i=edge[i].next){
int v = edge[i].to;
if(dis[v] > dis[u] + edge[i].val){
dis[v] = dis[u] + edge[i].val;
if(!vis[v]){
upd[v] += 1;
if(upd[v] > n){// 更新次数超过n次, 说明出现负环, 那么所有能够达到这个点的点都会无穷小
f[u] = 1;
Dfs(u);
}else{
q.push(v);
vis[v] = 1;
}
}
}
}
}
}
int cal(int x){
return x * x * x;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
for(int kase=1;kase<=t;kase++){
int n;
cin >> n;
memset(head, -1, sizeof head);
memset(f, 0, sizeof f);
memset(vis, 0, sizeof vis);
memset(upd, 0, sizeof upd);
memset(dis, 0x3f, sizeof dis);
cnt = 0;
for(int i=1;i<=n;i++) cin >> a[i];
int m, q;
cin >> m;
for(int i=0;i<m;i++){
int u, v;
cin >> u >> v;
Add_Edge(u, v, cal(a[v] - a[u]));
}
spfa(1, n);
cin >> q;
cout << "Case " << kase << ":\n";
while(q--){
int u;
cin >> u;
if(dis[u] == INF || dis[u] < 3 || f[u]){
cout << "?\n";
}else{
cout << dis[u] << '\n';
}
}
}
return 0;
}
#include
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
struct Edge{
int next;
int to;
ll val;
}edge[N << 2];
int cnt = 0;
int head[N];
ll dis[N];
const ll INF = 0x3f3f3f3f3f3f3f3f;
void Add_Edge(int u, int v, int w){
edge[cnt].next = head[u];
edge[cnt].to = v;
edge[cnt].val = w;
head[u] = cnt++;
}
struct st{
int id;
ll val;
st(){}
st(int id, ll val): id(id), val(val){}
bool operator <(const st &B)const{
return val > B.val;
}
};
bool vis[N];
ll dijkstra(int s, int n){
dis[s] = 0;
priority_queue<st> q;
q.push(st(s, dis[s]));
while(!q.empty()){
auto u = q.top();
q.pop();
if(vis[u.id]) continue;
vis[u.id] = 1;
for(int i=head[u.id];~i;i=edge[i].next){
int v = edge[i].to;
if(!vis[v] && dis[v] > dis[u.id] + edge[i].val){
dis[v] = dis[u.id] + edge[i].val;
q.push(st(v, dis[v]));
}
}
}
return dis[n];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
for(int kase=1;kase<=t;kase++){
int n, m;
ll c;
cin >> n >> m >> c;
cnt = 0;
memset(head, -1, sizeof head);
memset(vis, 0, sizeof vis);
memset(dis, 0x3f, sizeof dis);
for(int i=1;i<=n;i++){
int u;
cin >> u;
Add_Edge(i, u + n, 0);
if(u > 1){
Add_Edge(i, u + n - 1, c);
Add_Edge(u + n - 1, i, c);
}
if(u < n){
Add_Edge(i, u + n + 1, c);
Add_Edge(u + n + 1, i, c);
}
}
for(int i=1;i<=m;i++){
int u, v;
ll w;
cin >> u >> v >> w;
Add_Edge(u, v, w);
Add_Edge(v, u, w);
}
ll ans = dijkstra(1, n);
if(n == 0) ans = -1;
if(n == 1) ans = 0;
cout << "Case #" << kase << ": ";
cout << (ans == INF ? -1 : ans) << '\n';
}
return 0;
}
#include
using namespace std;
typedef long long ll;
const int N = 305;
int mp[N][N];
int dis[N];
int vis[N];
const int INF = 0x3f3f3f3f;
void spfa(int s, int n){
queue<int> q;
vis[s] = 1;
for(int i=1;i<=n;i++){
if(i == s){
dis[i] = INF;
}else{
q.push(i);
dis[i] = mp[s][i];
vis[i] = 1;
}
}
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = 0;
for(int i=1;i<=n;i++){
if(dis[i] > dis[u] + mp[u][i]){
dis[i] = dis[u] + mp[u][i];
if(!vis[i]){
vis[i] = 1;
q.push(i);
}
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
while(cin >> n){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin >> mp[i][j];
}
}
spfa(1, n);
int d1 = dis[1];
int d = dis[n];
spfa(n, n);
int d2 = dis[n];
cout << min(d1 + d2, d) << '\n';
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int N = 10005;
struct Edge{
int next;
int to;
int val;
}edge[3 * N];
int cnt = 0;
int head[N];
void Add_Edge(int u, int v, int w){
edge[cnt].next = head[u];
edge[cnt].to = v;
edge[cnt].val = w;
head[u] = cnt++;
}
int vis[N];
int dis[N];
int num[N];
const int INF = 0x3f3f3f3f;
int spfa(int s, int n){
queue<int> q;
q.push(s);
vis[s] = 1;
num[s] = 1;
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = 0;
for(int i=head[u];~i;i=edge[i].next){
int v = edge[i].to;
if(dis[v] > dis[u] + edge[i].val){
dis[v] = dis[u] + edge[i].val;
num[v] += 1;
if(num[v] >= n){
return -1;
}
if(!vis[v]){
q.push(v);
vis[v] = 1;
}
}
}
}
if(dis[n] == INF) return -2;
return dis[n];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, ML, MD;
cin >> n >> ML >> MD;
memset(head, -1, sizeof head);
for(int i=0;i<ML;i++){// 统一小的指向大的
int u, v, w;
cin >> u >> v >> w;
if(u > v) swap(u, v);// dis[v] - dis[u] <= w <=> dis[v] <= dis[u] + w <=> [u, v] = w
Add_Edge(u, v, w);
}
for(int i=0;i<MD;i++){
int u, v, w;
cin >> u >> v >> w;
if(u > v) swap(u, v);// dis[v] - dis[u] >= w <=> dis[u] <= dis[v] - w <=> [v, u] = -w
Add_Edge(v, u, -w);
}
for(int i=1;i<n;i++){
Add_Edge(i + 1, i, 0); // dis[i] <= dis[i + 1]
}
cout << spfa(1, n) << '\n';
return 0;
}