1780: 简单的图论问题?
#include<string>
#include<math.h>
#include<stdio.h>
#include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
#include<string.h>
#include <stdlib.h>
#include<iterator>
using namespace std;
typedef unsigned long long ll;
const int maxn = 505;
const int inf = 100000;
int vis1[maxn][maxn];
int vis2[maxn][maxn][4];
int a[maxn][maxn];
int n,m,r1,r2,c1,c2;
struct node{
int x,y,dir;
int step;
};
bool operator<(node a , node b){
return a.step > b.step;
}
int to[4][2] = {1,0,-1,0,0,1,0,-1};
int bfs1(){
if(c1 == c2&& r1 == r2)
return 0;
priority_queue<struct node> que;
memset(vis1,0,sizeof(vis1));
node no,nxt,now;
no.x = r1 ; no.y = c1 ; no.step = a[r1][c1];
que.push(no);
vis1[r1][c1] = 1;
while(!que.empty()){
now = que.top();
que.pop();
if(now.x == r2&&now.y == c2)
return now.step;
for(int i = 0 ; i < 4; i ++){
nxt.x = now.x + to[i][0];
nxt.y = now.y + to[i][1];
if(nxt.x>0&&nxt.x<=n&&nxt.y>0&&nxt.y<=m&&a[nxt.x][nxt.y]!=0&&!vis1[nxt.x][nxt.y]){
vis1[nxt.x][nxt.y] = 1;
nxt.step = now.step + a[nxt.x][nxt.y];
que.push(nxt);
}
}
}
return -1;
}
int bfs2(){
if(c1 == c2&& r1 == r2)
return 0;
priority_queue<struct node> que;
memset(vis2,0,sizeof(vis2));
node no,nxt,now;
no.x = r1 ; no.y = c1 ; no.step = a[r1][c1];
no.dir = -1;
que.push(no);
vis2[r1][c1][0] = vis2[r1][c1][1] = vis2[r1][c1][2] =vis2[r1][c1][3] = 1;
while(!que.empty()){
now = que.top();
que.pop();
if(now.x == r2&&now.y == c2)
return now.step;
for(int i = 0 ; i < 4; i ++){
nxt.x = now.x + to[i][0];
nxt.y = now.y + to[i][1];
if(now.dir==i)
continue;
if(nxt.x>0&&nxt.x<=n&&nxt.y>0&&nxt.y<=m&&a[nxt.x][nxt.y]!=0&&!vis2[nxt.x][nxt.y][i]){
vis2[nxt.x][nxt.y][i] = 1;
nxt.step = now.step + a[nxt.x][nxt.y];
nxt.dir = i;
que.push(nxt);
}
}
}
return -1;
}
int main()
{
int i,j;
char ch[5];
int k = 1;
while(~scanf("%d%d%d%d%d%d",&n,&m,&r1,&c1,&r2,&c2)){
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
cin>>ch;
if(ch[0] == '*')
a[i][j] = 0;
else
a[i][j] = atoi(ch);
}
}
cout<<"Case "<<k++<<": ";
int res1 = bfs1();
int res2 = bfs2();
printf("%d %d\n",res1,res2);
}
return 0;
}
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
char str[80];
const int INF = 999999999;
map<LL,vector<int> > mp;
int n,m,A,B;
LL charToLL(char *str){
LL ans = 0;
int p = 1,len = strlen(str);
for(int i=len-1;i>=0;i--){
if(str[i]=='1'){
ans+=p;
}
p*=2;
}
return ans;
}
int binary(LL val,int id){
vector <int> vec = mp[val];
int down = 0,up = vec.size()-1,r=-1,l=INF;
while(down<=up){
int mid = (down+up)>>1;
if(id-vec[mid]>=A){
r = mid;
down = mid+1;
}else up = mid-1;
}
down = 0,up = vec.size()-1;
while(down<=up){
int mid = (down+up)>>1;
if(id-vec[mid]<=B){
l = mid;
up = mid-1;
}else down = mid+1;
}
if(l>r) return 0;
return r-l+1;
}
int main(){
int t = 1;
while(scanf("%d%d%d%d",&m,&n,&A,&B)!=EOF){
mp.clear();
for(int i=0;i<m;i++){
str[i] = '1';
}
str[m]='\0';
LL k = charToLL(str),xorsum=0,ans = 0;
for(int i=1;i<=n;i++){
scanf("%s",str);
LL x = charToLL(str);
xorsum^=x;
if(xorsum==k&&i>=A&&i<=B) ans++;
ans+=binary(k^xorsum,i);
mp[xorsum].push_back(i);
}
printf("Case %d: %lld\n",t++,ans);
}
}
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define Len 200005
#define mod 360000
const int INF = 0x3f3f3f3f;
#define exp 1e-6
int main()
{
LL C,n,sum,w,a,b,c;
w(scanf("%lld%lld",&C,&n)>0)
{
sum=0;
w=0;
int flag=0;
w(n--)
{
scanf("%lld%lld%lld",&a,&b,&c);
if(flag)
continue;
if(sum<a)
{
flag=1;
continue;
}
sum=sum-a+b;
if(sum>C)
flag=1;
if(sum!=C && c) flag = 1;
if(!n && (sum || b || c)) flag = 1;
}
if(flag)
printf("impossible\n");
else
printf("possible\n");
}
}