D - Moves on Binary Tree
这个题就是直接模拟,只是需要使用deque双端队列,哪边进,哪边出操作好就好了。
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <unordered_map>
#include <math.h>
#include <algorithm>
#include <deque>
#define int long long
using namespace std;
const int N = 1e6+10;
const int mod = 1e9+7;
deque<int>st;
char a[N];
signed main()
{
int n,x;cin>>n>>x;
// getchar();
scanf("%s",a+1);
for(int i=1;i<=n;i++)
{
if(a[i]=='R')
st.push_back(1);
else if(a[i]=='L')
st.push_back(0);
else if(a[i]=='U'){
if(!st.empty()){
st.pop_back();
}
else{
x /= 2;
}
}
}
while(!st.empty())
{
if(st.front()==0){
x = 2*x;
}
else if(st.front()==1){
x = 2*x+1;
}
st.pop_front();
}
cout<<x<<endl;
return 0;
}
佛洛依德松弛之后,对每条边进行操作,如果可以去掉,那就去掉。
注意数组大小!!!!!
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <unordered_map>
#include <math.h>
#include <algorithm>
#include <deque>
#define int long long
using namespace std;
const int M = 1e4+10;
const int N = 300+10;
const int mod = 1e9+7;
int n,m;
int a[N][N];
struct node{
int x,y,val;
}edge[N*N];
signed main(){
scanf("%lld%lld",&n,&m);
memset(a,0x3f,sizeof(a));
// for(int i=1;i<=n;i++) a[i][i]=0;
for(int i=1;i<=m;i++){
int x,y,c;
scanf("%lld%lld%lld",&x,&y,&c);
a[x][y] = c;
a[y][x] = a[x][y];
edge[i] = {x,y,a[x][y]};
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
}
}
}
int ans=0,tem=0;
// int ans = 0;
for(int i = 1; i <= m; i++){
tem=0;
for(int j = 1; j <= n; j++){
if(a[edge[i].x][j] + a[j][edge[i].y] <= edge[i].val || a[edge[i].y][j] + a[j][edge[i].x] <= edge[i].val) tem = 1;
}
ans += tem;
}
cout<<ans<<endl;
return 0;
}