Input
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
Sample Input
2 1 #. .# 4 4 ...# ..#. .#.. #... -1 -1
Sample Output
2 1
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int visit[20]; char mp[20][20]; int ans; int n,k; int DFS(int x,int y){ if(y>=k){ ans++; return 0; } for(int i=x;i<n;i++){ for(int j=0;j<n;j++){ if(!visit[j]&&mp[i][j]=='#'){ visit[j]=true; DFS(i+1,y+1); visit[j]=false; } } } return 0; } int main(){ while(scanf("%d%d",&n,&k)&&n!=-1){ memset(visit,false,sizeof(visit)); memset(mp,false,sizeof(mp)); for(int i=0;i<n;i++){ cin>>mp[i]; } ans=0; DFS(0,0); cout<<ans<<endl; } return 0; }
2.poj2387
Description
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
Output
Sample Input
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100
Sample Output
90
#include<cstdio> #include<iostream> #include<cstring> using namespace std; const int INF=1<<29; int n,m; int w[2005][2005],vis[2005],p[2005],dis[2005]; void djs(int f){ for(int i=1;i<=n;i++){ dis[i]=w[f][i]; vis[i]=0; p[i]=0; } dis[f]=0; vis[f]=1; for(int i=1;i<=n;i++){ int ans=INF,k; for(int j=1;j<=n;j++){ if(!vis[j]&&dis[j]<ans){ ans=dis[j]; k=j; } } vis[k]=1; if(k==0) break; for(int j=1;j<=n;j++){ if(!vis[j]&&dis[k]+w[k][j]<dis[j]){ dis[j]=dis[k]+w[k][j]; p[j]=k; } } } } int main(){ cin>>m>>n; for(int i=1;i<=n;i++){ w[i][i]=INF; for(int j=1;j<=n;j++){ w[j][i]=w[i][j]=INF; } } for(int i=0;i<m;i++){ int u,v,weight; cin>>u>>v>>weight; if(w[u][v]>weight){ w[u][v]=w[v][u]=weight; } } djs(1); cout<<dis[n]<<endl; return 0; }
poj2251
Description
Is an escape possible? If yes, how long will it take?
Input
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
三维路径
#include<iostream> #include<cstdio> #include<cmath> #include<queue> #include<cstring> using namespace std; char mp[35][35][35]; bool vis[35][35][35]; int l,r,c,sx,sy,sz,ex,ey,ez; int dx[6]={1,-1,0,0,0,0}; int dy[6]={0,0,1,-1,0,0}; int dz[6]={0,0,0,0,1,-1}; struct node{ int x,y,z,step; }; int check(int x,int y,int z){ if(x<0||y<0||z<0||x>=l||y>=r||z>=c||(mp[x][y][z]=='#')||(vis[x][y][z])) return 1; return 0; } int bfs(){ node a,next; queue<node> q; a.x=sx; a.y=sy; a.z=sz; a.step=0; vis[sx][sy][sz]=1; q.push(a); while(!q.empty()){ a=q.front(); q.pop(); if(a.x==ex&&a.y==ey&&a.z==ez){ return a.step; } for(int i=0;i<6;i++){ next=a; next.x=a.x+dx[i]; next.y=a.y+dy[i]; next.z=a.z+dz[i]; if(check(next.x,next.y,next.z)) continue; vis[next.x][next.y][next.z]=1; next.step=a.step+1; q.push(next); } } return 0; } int main(){ while(cin>>l>>r>>c){ if(l==0&&r==0&&c==0) break; for(int i=0;i<l;i++){ for(int j=0;j<r;j++){ cin>>mp[i][j]; for(int k=0;k<c;k++){ if(mp[i][j][k]=='S'){ sx=i; sy=j; sz=k; } if(mp[i][j][k]=='E'){ ex=i; ey=j; ez=k; } } } } memset(vis,0,sizeof(vis)); int ans; ans=bfs(); ///cout<<ans<<endl; if(ans) cout<<"Escaped in "<<ans<<" minute(s)."<<endl; else cout<<"Trapped!"<<endl; } return 0; }
The Marjar Empire has N cities (including the capital), indexed from 0 to N - 1 (the capital is 0) and there are M highways can be built. Building the i-th highway costs Ci dollars. It takes Di minutes to travel between city Xi and Yi on the i-th highway.
Edward wants to find a construction plan with minimal total time needed to reach other cities from the capital, i.e. the sum of minimal time needed to travel from the capital to city i (1 ≤ i ≤ N). Among all feasible plans, Edward wants to select the plan with minimal cost. Please help him to finish this task.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first contains two integers N, M (1 ≤ N, M ≤ 105).
Then followed by M lines, each line contains four integers Xi, Yi, Di, Ci (0 ≤ Xi, Yi < N, 0 < Di, Ci < 105).
Output
For each test case, output two integers indicating the minimal total time and the minimal cost for the highway project when the total time is minimized.
Sample Input
2 4 5 0 3 1 1 0 1 1 1 0 2 10 10 2 1 1 1 2 3 1 2 4 5 0 3 1 1 0 1 1 1 0 2 10 10 2 1 2 1 2 3 1 2
Sample Output
4 3 4 4
从0到n的最短路及最小花费
#include<iostream> #include<cstdio> #include<vector> #include<cmath> #include<queue> #include<cstring> using namespace std; typedef long long ll; const int N=1005; const int INF=1000000000; const ll maxx=1000000000000005; struct node{ int to,nxt; ll c,d; }e[N*200]; int n,m,cnt; ll d[N*100]; int head[N*100]; vector<int>pre[N*100]; void add(int u,int v,ll d,ll c){ e[cnt].to=v; e[cnt].c=c; e[cnt].d=d; e[cnt].nxt=head[u]; head[u]=cnt++; } void solve(){ for(int i=0;i<n;i++){ pre[i].clear(); } fill(d,d+n,maxx); d[0]=0; queue<int>q; 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<n;i++){ ans1+=d[i]; ll minn=maxx; for(int j=0;j<pre[i].size();j++){ minn=min(minn,e[pre[i][j]].c); } if(minn==maxx) continue; ans2+=minn; } printf("%lld %lld\n",ans1,ans2); } int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); cnt=0; memset(head,-1,sizeof(head)); int a,b; ll c,d; for(int i=0;i<m;i++){ scanf("%d%d%lld%lld",&a,&b,&d,&c); add(a,b,d,c); add(b,a,d,c); } solve(); } return 0; }
5. Poj3278 Catch That Cow
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Line 1: Two space-separated integers: N and K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4
Hint
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
#include<iostream> #include<cstdio> #include<vector> #include<cmath> #include<queue> #include<cstring> #include<string> using namespace std; typedef long long ll; int n,k; const int N=100010; int step[N],vis[N]; queue<int>q; int bfs(int n,int k){ int now,next; step[n]=0; vis[n]=1; q.push(n); while(!q.empty()){ now=q.front(); q.pop(); for(int i=0;i<3;i++){ if(i==0) next=now-1; else if(i==1) next=now+1; else if(i==2) next=now*2; if(next<0||next>N) continue; if(!vis[next]){ vis[next]=1; q.push(next); step[next]=step[now]+1; } if(next==k) return step[next]; } } } int main(){ scanf("%d%d",&n,&k); if(n>k){ printf("%d\n",n-k); } else{ printf("%d\n",bfs(n,k)); } return 0; }
hdu同题版
#include<iostream> #include<cstdio> #include<vector> #include<cmath> #include<queue> #include<cstring> #include<string> using namespace std; typedef long long ll; int n,k; const int N=100010; int step[N],vis[N]; queue<int>q; int bfs(int n,int k){ while(!q.empty()) q.pop(); int now,next; step[n]=0; vis[n]=1; q.push(n); while(!q.empty()){ now=q.front(); q.pop(); for(int i=0;i<3;i++){ if(i==0) next=now-1; else if(i==1) next=now+1; else if(i==2) next=now*2; if(next<0||next>N) continue; if(!vis[next]){ vis[next]=1; q.push(next); step[next]=step[now]+1; } if(next==k) return step[next]; } } } int main(){ while(~scanf("%d%d",&n,&k)){ memset(vis , 0 , sizeof(vis)); if(n>k){ printf("%d\n",n-k); } else{ printf("%d\n",bfs(n,k)); } } return 0; }
法二:
#include<iostream> #include<cstdio> #include<vector> #include<cmath> #include<queue> #include<cstring> #include<string> using namespace std; typedef long long ll; int n,k; const int N=100010; bool vis[N]; struct node{ int x,step; }; node q[N]; int bfs(){ node now,next; int head,tail; head=tail=0; q[tail].x=n; q[tail].step=0; tail++; vis[n]=true; while(head<tail){ now=q[head]; head++; for(int i=0;i<3;i++){ if(i==0) next.x=now.x-1; else if(i==1) next.x=now.x+1; else next.x=2*now.x; if(next.x<0||next.x>=N) continue; if(!vis[next.x]){ vis[next.x]=true; next.step=now.step+1; q[tail].x=next.x; q[tail].step=next.step; tail++; if(next.x==k) return next.step; } } } } int main(){ while(scanf("%d%d",&n,&k)!=EOF){ memset(vis,false,sizeof(vis)); if(n>=k) printf("%d\n",n-k); else printf("%d\n",bfs()); } return 0; }