diff --git a/HDOJ/1010.cpp b/HDOJ/1010.cpp new file mode 100644 index 0000000..6cafe59 --- /dev/null +++ b/HDOJ/1010.cpp @@ -0,0 +1,130 @@ +/// TLE -> WA +#include +#include +#include +#include +using namespace std; + +#define MAXN 10 +#define MAXM 10 + +char map[MAXN][MAXM]; + +int N,M,T; + +inline void init() +{ + memset(map,0,MAXN*MAXM); +} + +inline bool checkmap() +{ + int wall=0; + for(int i=0;iN||y<1||y>M) return; + if(x==endx&&y==endy&&step==T) + { + endflag=true; + return; + } + /// TLE? Try this + int temp=T-step-(abs(endx-x)+abs(endy-y)); + if(temp<0||temp%2) + { + return; + } + if(map[x-1][y]=='.'||map[x-1][y]=='D') + { + map[x-1][y]='X'; + DFS(x-1,y,step+1); + map[x-1][y]='.'; + } + if(map[x+1][y]=='.'||map[x+1][y]=='D') + { + map[x+1][y]='X'; + DFS(x+1,y,step+1); + map[x+1][y]='.'; + } + if(map[x][y-1]=='.'||map[x][y-1]=='D') + { + map[x][y-1]='X'; + DFS(x,y-1,step+1); + map[x][y-1]='.'; + } + if(map[x][y+1]=='.'||map[x][y+1]=='D') + { + map[x][y+1]='X'; + DFS(x,y+1,step+1); + map[x][y+1]='.'; + } +} + + +int main() +{ + while(scanf("%d %d %d",&N,&M,&T)==3&&N+M+T!=0) + { + init(); + for(int i=1;i<=N;i++) + { + scanf("%s",&map[i][1]); + } + if(!checkmap()) + { + printf("NO\n"); + continue; + } + findSD(&startx,&starty,&endx,&endy); + endflag=false; + DFS(startx,starty,0); + if(endflag) + { + printf("YES\n"); + } + else + { + printf("NO\n"); + } + } + return 0; +} diff --git a/HDOJ/1010_csdn.cpp b/HDOJ/1010_csdn.cpp new file mode 100644 index 0000000..56390a8 --- /dev/null +++ b/HDOJ/1010_csdn.cpp @@ -0,0 +1,73 @@ +#include +#include +using namespace std; +char a[10][10]; +int n,m,t; +int sx,sy,ex,ey; +bool flag; +int dir[4][2]={0,1,1,0,0,-1,-1,0}; +int abs(int x) +{ + return x<0?-x:x; +} +void DFS(int x,int y,int time)//当前位置坐标(x,y),到目前位置消耗时间 +{ + if(x<0||x>=n||y<0||y>=m) + return; + if(x==ex&&y==ey&&time==t) + { + flag =true; + return ; + } + if(flag) + return; + int temp=(t-time)-(abs(x-ex)+abs(y-ey)); + if(temp<0||temp&1) + return; + for(int i=0;i<4;i++) + { + int xx=x+dir[i][0]; + int yy=y+dir[i][1]; + if(a[xx][yy]!='X') + { + a[xx][yy]='X'; + DFS(xx,yy,time+1); + a[xx][yy]='.'; + if(flag) + return ; + } + } +} +int main() +{ + while(cin>>n>>m>>t) + { + if(n==0&&m==0) + break; + int wall=0; + for(int i=0; i>a[i][j]; + if(a[i][j]=='S') + sx=i,sy=j; + else if(a[i][j]=='D') + ex=i,ey=j; + else if(a[i][j]=='X') + wall++; + } + if(n*m-wall<=t) + { + cout<<"NO"< +#include +#include +#include +#include +#include +using namespace std; + +#define MAXN 105 +int father[MAXN]; +int noderank[MAXN]; + +void init() +{ + for(int i=0;inoderank[fb]) + { + father[fb]=fa; + } + else + { + father[fa]=fb; + if(noderank[fa]==noderank[fb]) + { + noderank[fb]++; + } + } +} + +struct pack +{ + int a,b,v; +}; +#define MAXM 5000 +pack pk[MAXM]; + +bool cmp(const pack& a,const pack& b) +{ + return a.v +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +//Constant Declaration +/*--------------------------*/ +//#define LL long long +#define LL __int64 +const int M=100001; +const int INF=1<<30; +const double EPS = 1e-11; +const double PI = acos(-1.0); +/*--------------------------*/ +// some essential funtion +/*----------------------------------*/ +void Swap(int &a,int &b){ int t=a;a=b;b=t; } +int Max(int a,int b){ return a>b?a:b; } +int Min(int a,int b){ return a 0) + { + sum += c[k]; + k -= LowBit(k); + } + return sum; +} + +void Update(int k, int sc) +{ + while (k <= 100000) + { + c[k] += sc; + k += LowBit(k); + } +} + + + +int main() +{ + //freopen("in.txt","r",stdin); + //freopen("out.txt","w",stdout); + int t, case1 = 0; + scanf("%d", &t); + int m; + int i, j; + int num; + //scanf("%d%d", &n, &m); + while (t--) + { + scanf("%d", &num); + memset(c, 0, sizeof(c)); + memset(left_lower, 0, sizeof(left_lower)); + memset(right_lower, 0, sizeof(right_lower)); + for (i = 1; i <= num; i++) + { + scanf("%d", &a[i]); + left_lower[i] += Sum(a[i] - 1);///Sum(a[i] - 1):插入该数前,区间1到a[i]-1的总个数 + Update(a[i], 1); + } + + memset(c, 0, sizeof(c)); + for (i = num; i > 0; i--)//顺序插入 + { + right_lower[i] += Sum(a[i] - 1); + Update(a[i], 1); + } + + LL ans = 0; + + for (i = 1; i <= num; i++)//逆序插入 + { + ans += left_lower[i]*(num - i - right_lower[i]);//由于只能求比其小的个数,可以用i右边的总个数见减比他小的来求比他大的个数。 + ans += (i - 1 - left_lower[i])*right_lower[i];// + } + + printf("%I64d\n", ans); + } + + + return 0; +} diff --git a/HDOJ/3790_cnblogs.cpp b/HDOJ/3790_cnblogs.cpp new file mode 100644 index 0000000..6a9bb4f --- /dev/null +++ b/HDOJ/3790_cnblogs.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +//Constant Declaration +/*--------------------------*/ +//#define LL long long +#define LL __int64 +const int M=1100;//最多点数 +const int INF=1<<30; +const double EPS = 1e-11; +const double PI = acos(-1.0); +/*--------------------------*/ +// some essential funtion +/*----------------------------------*/ +void Swap(int &a,int &b){ int t=a;a=b;b=t; } +int Max(int a,int b){ return a>b?a:b; } +int Min(int a,int b){ return a d[min_num].l + g[min_num][i].l) + { + d[i].l = d[min_num].l + g[min_num][i].l; + d[i].p = d[min_num].p + g[min_num][i].p; + } + if (!used[i] && d[i].l == d[min_num].l + g[min_num][i].l && d[i].p > d[min_num].p + g[min_num][i].p)//这里的判断是关键 + { + d[i].p = d[min_num].p + g[min_num][i].p; + } + } + + } + return d[end].l; +} + + + + + +int main() +{ + //freopen("in.txt","r",stdin); + //freopen("out.txt","w",stdout); + //int t, case1 = 0; + //scanf("%d", &t); + int n, m; + int i, j; + while (scanf("%d%d", &n, &m), n + m) + { + init(n); + for (i = 0; i < m; i++) + { + int a, b, c, c1; + scanf("%d%d%d%d", &a, &b, &c, &c1); + if (g[a][b].l > c) + { + g[b][a].l = g[a][b].l = c; + g[b][a].p= g[a][b].p = c1; + }//此题为无向图 + if (g[a][b].l == c && g[b][a].p > c1)//这里的判断是关键 + { + g[b][a].p= g[a][b].p = c1; + } + + } + int star, end,ans; + scanf("%d%d", &star, &end); + ans = dijkstra(star, end, n); + printf("%d %d\n", ans, d[end].p); + } + + return 0; +} diff --git a/HDOJ/4786_cnblogs.cpp b/HDOJ/4786_cnblogs.cpp new file mode 100644 index 0000000..afe505a --- /dev/null +++ b/HDOJ/4786_cnblogs.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#define maxn 100100 +using namespace std; + +struct edge{ + int u,v; +}a[2][maxn]; + +int s[2]; +int n,m,c,t,uu,vv,cc,MM,mm,cas=0; +int f[maxn],g[maxn]; +bool flag; + +void init() { for (int i=1; i<=n; i++) f[i]=i; } + +void build(int xx,int yy) { f[f[xx]]=f[yy]; } + +int getf(int x) +{ + if (f[x]!=x) f[x]=getf(f[x]); + return f[x]; +} + +void addedge(int x) +{ + int tot=0; + init(); + for (int i=1; i<=s[x]; i++) + if (getf(a[x][i].u)!=getf(a[x][i].v)) tot++,build(a[x][i].u,a[x][i].v); + if (x==0) mm=n-1-tot; + else MM=tot; +} + +int main() +{ + g[1]=1,g[2]=2; + for (int i=2; g[i]<=maxn; i++) g[i+1]=g[i]+g[i-1]; + scanf("%d",&t); + while (t--) + { + scanf("%d%d",&n,&m); + s[0]=s[1]=0; + for (int i=1; i<=m; i++) + { + scanf("%d%d%d",&uu,&vv,&cc); + a[cc][++s[cc]].u=uu; + a[cc][s[cc]].v=vv; + } + init(); + for (int i=1; i<=s[0]; i++) + if (getf(a[0][i].u)!=getf(a[0][i].v)) build(a[0][i].u,a[0][i].v); + for (int i=1; i<=s[1]; i++) + if (getf(a[1][i].u)!=getf(a[1][i].v)) build(a[1][i].u,a[1][i].v); + flag=true; + for (int i=2; i<=n; i++) + if (getf(i)!=getf(i-1)) { flag=false; break; } + if (!flag) + { + printf("Case #%d: No\n",++cas); + continue; + } + addedge(0),addedge(1); + //cout<<" MM && mm : " <=mm) { flag=true; break; } + if (flag) printf("Case #%d: Yes\n",++cas); + else printf("Case #%d: No\n",++cas); + } + return 0; +} diff --git a/UVALive/7483.cpp b/UVALive/7483.cpp new file mode 100644 index 0000000..949a33c --- /dev/null +++ b/UVALive/7483.cpp @@ -0,0 +1,50 @@ +/** +LINK= +http://acm.hust.edu.cn/vjudge/contest/view.action?cid=115784#problem/F +*/ +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAXN 105 + +int main() +{ + int n; + while(scanf("%d",&n)==1) + { + vector pool[MAXN*2+8]; + for(int i=0;i +using namespace std; + +#define pb push_back + +int main() +{ + int N; + while(cin>>N){ + vector arr[201]; + for (int i=1;i<=N;i++){ + int a, b; + scanf("%d%d",&a,&b); + arr[a].pb(b); + } + int mn=2e9, ans=0; + for (int i=1;i<=N+N;i++){ + if (mn < i) ans++, mn = 2e9; + for(int j=0;j +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +using namespace std; +# define LL long long + +const int N=1005; +const int INF=1000000000; +const LL oo=1000000000000005; + +struct Edge +{ + int to,nxt; + LL c,d; +}; + +int n,m,cnt; +Edge e[N*200]; +LL d[N*100]; +int head[N*100]; +vectorpre[N*100]; + +void add(int u,int v,LL d,LL c) +{ + e[cnt].to=v; + e[cnt].d=d; + e[cnt].c=c; + e[cnt].nxt=head[u]; + head[u]=cnt++; +} + +void solve() +{ + for(int i=0;iq; + q.push(0); + while(!q.empty()) + { + int u=q.front(); + q.pop(); + for(int i=head[u];i!=-1;i=e[i].nxt){ + int v=e[i].to; + if(d[v]>d[u]+e[i].d){ + d[v]=d[u]+e[i].d; + q.push(v); + } + } + } + + q.push(0); + while(!q.empty()){ + int u=q.front(); + q.pop(); + for(int i=head[u];i!=-1;i=e[i].nxt){ + int v=e[i].to; + if(d[v]==d[u]+e[i].d){ + pre[v].push_back(i); + q.push(v); + } + } + } + + LL ans1=0,ans2=0; + for(int i=0;i