浙大机试

/1001
#include<stdio.h>
int main()
{
  int a,b;
  int sum;
  while(scanf("%d%d\n",&a,&b) != EOF){
        sum = a+b;
	if(sum < 0){
	printf("-");
	sum = -sum;
	}
    if(sum>=1000000){
        printf("%d,%03d,%03d\n",sum/1000000, (sum/1000)%1000, sum%1000);
    }
    else if(sum >= 1000){
        printf("%d,%03d\n",sum/1000,sum%1000);
    } else{
        printf("%d\n", sum);
    }
  }
  return 0;
}
第一:最短路径(Dijstra算法):
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
//Dijkstra算法:(1)初始化
//(2)遍历与集合k结点直接相邻的边(U,v,c),其中U属于集合K,V不属于集合k,计算由结点1出发按照已经得到的最短路径到达U,再由U经过改编到达V时
//的最短路径,比较所有域集合k中结点直接相邻的非集合k的结点该路径长度,其中路径长度最小的结点被确定为下一个最短路径确定的结点,其最短轮径长度即为
//这个路径长度,最后将该点加入集合k. 
//(3)若集合已经包含了所有的点,算法结束;否则重复步骤(2) 
struct E{
    int next;
    int c;
};
vector<E>edge[501];
bool mark[501];
int dis[501];
int team[501];
int main()
{  
    int i,j,n,m,c1,c2;
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    while(scanf("%d%d%d%d",&n,&m,&c1,&c2)!=EOF){
      for(i=1;i<=n;i++) edge[i].clear();//初始化邻接链表      
      for(i=1;i<=n;i++){
         dis[i]=-1;
         mark[i]=false;
       }                                        
      for(i=0;i<n;i++)
        scanf("%d",&team[i]);
      while(m--){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            E tmp;
            tmp.c=c;
            tmp.next=b;
            edge[a].push_back(tmp);
            tmp.next=a;
            edge[b].push_back(tmp);
       }                                               
      dis[c1]=0;//得到最近的点为结点c1,长度为0 
      mark[c1]=true;//将结点c1加入集合k 
      int newp=c1;//集合中新加入的点结点为c1 
      for(i=1;i<n;i++){
          for(j=0;j<edge[newp].size();j++){
              int t=edge[newp][j].next;
              int c=edge[newp][j].c;
              if(mark[t]==true) continue;
              if(dis[t]==-1||dis[t]>dis[newp]+c){
                  dis[t]=dis[newp]+c;
              }
          }  
          int min=12131313;
          for(j=0;j<n;j++){//遍历所有结点 
             if(mark[i]==1 || dis[j]==-1) continue; 
             if(dis[j]<min){
                 min=dis[j];
                 newp=j;
             }
         }
           mark[newp]=true;
      }
      printf("%d\n",dis[c2]);
    }
    return 0;
}
第二:最短路径(Floyd算法)
#include<stdio.h>
int ans[101][101];
int main()
{
   int n,m,i,j,k;
   while(scanf("%d%d",&n,&m)!=EOF){
       if(n==0 && m==0) break;
       for(i=1;i<=n;i++){
           for(j=1;j<=n;j++){
               ans[i][j]=-1;
           }
           ans[i][i]=0;   
       }
       while(m--){
           int a,b,c;
           scanf("%d%d%d",&a,&b,&c);
           ans[b][a]=ans[a][b]=c;
       }
       for(k=1;k<=n;k++){
           for(i=1;i<=n;i++){
               for(j=1;j<=n;j++){
                   if(ans[i][k]==-1||ans[k][j]==-1) continue;
                   if(ans[i][i]==-1|| ans[i][k]+ans[k][j]<ans[i][j]){
                       ans[i][j]=ans[i][k]+ans[k][j];
                   }
               }
           }
       }
       printf("%d\n",ans[1][n]);
   }
    return 0;

第三:0-1背包问题
采药
#include<stdio.h>
#define INF 0x7fffffff
int max(int a,int b){return a>b? a:b;}
struct E{
    int w;
	int v;
}list[101];
int dp[101][101];//记录状态数组,dp[i][j]表示前i个物品组成的总体积不大于j的最大值和

int main()
{
    int s,n,i,j;
	while(scanf("%d%d",&s,&n)!=EOF){
	     for(i=1;i<=n;i++)
			 scanf("%d%d",&list[i].w,&list[i].v);
	     for(i=0;i<=s;i++)
			 dp[0][i]=0;
		 for(i=1;i<=n;i++){
			 for(j=s;j>=list[i].w;j--){
			      dp[i][j]=max(dp[i-1][j],dp[i-1][j-list[i].w]+list[i].v);
			 
			 }
		    for(j=list[i].w-1;j>=0;j--)//对list[i].w-1到0 的每个状态仅能源于dp[i-1][j],故直接赋值
				dp[i][j]=dp[i-1][j];
		 
		 }
	   printf("%d\n",dp[n][s]);
	}
   return 0;

}
Pat 1003
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.
Input
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.
Output
For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.
Sample Input
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Sample Output
2 4
解答:
方法一:DFS
#include<stdio.h>
int road[510][510],vst[510],hper[510];
int N,M,C1,C2,shpath=-1,maxhp=-1,cnt=0;
int main(){
  void dfs(int from,int len,int helpers);
  int i,j;
  int c1,c2,l;
  scanf("%d %d %d %d",&N,&M,&C1,&C2);
  for(i=0;i<N;i++){
	scanf("%d",&hper[i]);
	vst[i]=0;
	for(j=0;j<N;j++){
		road[i][j]=-1;
		road[j][i]=-1;
	}
  }

  for(i=0;i<M;i++){
	scanf("%d %d %d",&c1,&c2,&l);
	road[c1][c2]=l;
	road[c2][c1]=l;
  }
  vst[C1]=1;
  dfs(C1,0,hper[C1]);
  printf("%d %d\n",cnt,maxhp);
  return 0;
}
void dfs(int from,int len,int helpers){
  int i,j;
  if(len>shpath&&shpath!=-1){
	return;
  }
  if(from==C2){
	  if(len<shpath||shpath==-1){
		shpath=len;
		maxhp=helpers;
		cnt=1;
	  }
	  else if(len==shpath){
		cnt++;
		if(helpers>maxhp){
			maxhp=helpers;
		}
	  }
	return;
  }
  for(i=0;i<N;i++){
	  if(vst[i]==0&&road[from][i]!=-1){
		vst[i]=1;
		dfs(i,len+road[from][i],helpers+hper[i]);
	    vst[i]=0;
	  }
  }
}
方法二:Dijstra算法代码
#include<stdio.h>
#define maxint 1<<28
int N,M,C1,C2;
int map[510][510],vst[510],dis[510],hel[510];
int pathcnt[510],pathhel[510];
void init(){
	int i,j;
	for(i=1;i<=N;i++){
		vst[i]=0;
		hel[i]=0;
		dis[i]=maxint;
		pathcnt[i]=0;
		pathhel[i]=0;
		for(j=1;j<=N;j++){
			map[i][j]=map[j][i]=maxint;
		}
	}
}
void Dijstra(){
	int i,j,k;
	dis[C1]=0;
	pathcnt[C1]=1;
	pathhel[C1]=hel[C1]; 
	for(i=1;i<=N;i++){
		int imin=maxint;
		for(j=1;j<=N;j++){
			if(!vst[j]&&dis[j]<imin){
				imin=dis[j];
				k=j;
			}
		}
		vst[k]=1;
		for(j=1;j<=N;j++){
			if(vst[j]==0){ 
				if(dis[j]>dis[k]+map[k][j]){
					dis[j]=dis[k]+map[k][j];
					pathhel[j]=pathhel[k]+hel[j];
					pathcnt[j]=pathcnt[k];
				}
				else if(dis[j]==dis[k]+map[k][j]){
					pathcnt[j]+=pathcnt[k];
					if(pathhel[j]<pathhel[k]+hel[j]){
						pathhel[j]=pathhel[k]+hel[j];	
					}
				}	
			}
			
		}
		if(imin==maxint||k==C2) return;
	}

}
int main(){
	int i,j,from,to,dist;
	scanf("%d %d %d %d",&N,&M,&C1,&C2);
	C1++;
	C2++;
	init();
	for(i=1;i<=N;i++){
		scanf("%d",&hel[i]);	
	}
	for(i=1;i<=M;i++){
		scanf("%d %d %d",&from,&to,&dist);
		map[from+1][to+1]=map[to+1][from+1]=dist;
	}
	Dijstra();
	printf("%d %d\n",pathcnt[C2],pathhel[C2]);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值