temp

1003 Emergency (25 分)(dijkstra单源最短路径)

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 Specification:

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, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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
#include<iostream>
#include<cmath>
#include<string.h>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;

int n,m,inf=0x3f3f3f3f,c1,c2;
int Map[510][510],w[510],weight[510],num[510],vis[510],dis[510];


void dijkstra(int s){
    fill(dis,dis+510,inf);
    dis[s] = 0;
    w[s] = weight[s];
    num[s] = 1;
    while(1)
    {
        int u = -1,MIN=inf;
        for(int i=0;i<n;i++)
        {
            if(!vis[i]&&dis[i]<MIN)
            {
                MIN = dis[i];
                u = i;
            }
        }
        if(u == -1)break;
        vis[u] = true;
        for(int i=0;i<n;i++)
        {
            if(!vis[i]&&Map[u][i])
            {
                if(dis[u] + Map[u][i] <dis[i])
                {
                   dis[i] = dis[u] + Map[u][i];
                    num[i] = num[u];
                    w[i] = w[u] +weight[i];
                }
                else if(dis[u] + Map[u][i] == dis[i]){
                    if(w[u] + weight[i] > w[i])w[i] = w[u] +weight[i];
                    num[i] +=num[u];
                }
            }
        }
    }
}
int main(){
    int t1,t2,t3;
    scanf("%d%d%d%d",&n,&m,&c1,&c2);
    for(int i=0;i<n;i++)
    {
        cin>>weight[i];
    }
    for(int i=0;i<m;i++)
    {
        cin>>t1>>t2>>t3;
        Map[t1][t2]=t3;
        Map[t2][t1]=t3;
    }
    dijkstra(c1);
    printf("%d %d", num[c2], w[c2]);
}

1007 Maximum Subsequence Sum (25 分)(动态规划求最大连续子序列)

Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4
const int maxn = 10010;
int a[maxn],dp[maxn];

int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    dp[0] = 0;
    for(int i=1;i<=n;i++)
    {
        dp[i]=max(a[i],a[i]+dp[i-1]);
    }
    int res = -0x3f3f3f;
    for(int i=1;i<=n;i++)
    {
        if(dp[i]>res){
            res = dp[i];
        }
    }
    cout<<res;
}

 

1009 Product of Polynomials (25 分)

This time, you are supposed to find A×B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10, 0≤N​K​​<⋯<N​2​​<N​1​​≤1000.

Output Specification:

For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 3 3.6 2 6.0 1 1.6
int main(){
    int n1,n2,a,cnt = 0;
    scanf("%d", &n1);
    double b,arr[1001] = {0.0},ans[2001] = {0.0};
    for(int i=0;i < n1;i++)
    {
        scanf("%d %lf", &a,&b);
        arr[a] = b;
    }
    scanf("%d",&n2);
    for(int i = 0; i< n2;i++){
        scanf("%d %lf",&a,&b);
        for(int j = 0;j < 1001;j++)
            ans[j+a] +=arr[j]*b;
    }
    for(int i = 2000;i >= 0; i--)
        if(ans[i] != 0.0)cnt++;
    printf("%d",cnt);
    for(int i = 2000;i >= 0;i--)
    {
        if(ans[i]!=0.0)
            printf(" %d %.1f",i,ans[i]);
    }
    return 0;
}

 

1013 Battle Over Cities (25 分)(dfs求联通分量)

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

For example, if we have 3 cities and 2 highways connecting city​1​​-city​2​​ and city​1​​-city​3​​. Then if city​1​​is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​-city​3​​.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

Output Specification:

For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

Sample Input:

3 2 3
1 2
1 3
1 2 3

Sample Output:

1
0
0
#include<iostream>
#include<cmath>
#include<string.h>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
int n,m,k,a,b;
int v[1010][1010];
bool vis[1010];
void dfs(int node)
{
    vis[node] = true;
    for(int i=1;i<=n;i++)
    {
        if(!vis[i]&&v[node][i] == 1)
        {
            dfs(i);
        }
    }
}
int main(){
    cin>>n>>m>>k;
    for(int i = 0;i<m;i++)
    {
        scanf("%d%d",&a,&b);
        v[a][b] = v[b][a] = 1;
    }
    for(int i=0;i<k;i++)
    {
        scanf("%d",&a);
        fill(vis,vis+1010,false);
        vis[a] = true;
        int cnt = 0;
        for(int j=1;j<=n;j++)
        {
            if(vis[j]==false)
            {
                dfs(j);
                cnt++;
            }
        }
        cout<<cnt-1<<endl;
    }
}

 

1017 Queueing at Bank (25 分)

Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there is a window available. It is assumed that no window can be occupied by a single customer for more than 1 hour.

Now given the arriving time T and the processing time P of each customer, you are supposed to tell the average waiting time of all the customers.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤10​4​​) - the total number of customers, and K (≤100) - the number of windows. Then N lines follow, each contains 2 times: HH:MM:SS - the arriving time, and P - the processing time in minutes of a customer. Here HH is in the range [00, 23], MM and SS are both in [00, 59]. It is assumed that no two customers arrives at the same time.

Notice that the bank opens from 08:00 to 17:00. Anyone arrives early will have to wait in line till 08:00, and anyone comes too late (at or after 17:00:01) will not be served nor counted into the average.

Output Specification:

For each test case, print in one line the average waiting time of all the customers, in minutes and accurate up to 1 decimal place.

Sample Input:

7 3
07:55:00 16
17:00:01 2
07:59:59 15
08:01:00 60
08:00:00 30
08:00:02 2
08:03:00 10

Sample Output:

8.2
struct node{
    int come,time;
}tempcustomer;
bool cmp1(node a,node b){
    return a.come<b.come;
}
int main(){
    int n,k;
    cin>>n>>k;
    vector<node>custom;
    for(int i = 0 ; i < n;i++)
    {
        int hh,mm,ss,time;
        scanf("%d:%d:%d %d",&hh,&mm,&ss,&time);
        int cometime = hh*3600+mm*60+ss;
        if(cometime>61200)continue;
        tempcustomer = {cometime,time*60};
        custom.push_back(tempcustomer);
    }
    sort(custom.begin(),custom.end(),cmp1);
    vector<int>window(k,28800);//全部赋值
    double result = 0.0;
    for(int i = 0;i<custom.size();i++)
    //第几个客户
    {
      //找到最快的窗口
      int tempindex = 0,minfinish = window[0];
      for(int j = 1;j<k;j++)
      {
          if(minfinish > window[j]){
            minfinish = window[j];
            tempindex = j;
          }
      }
      //求出这个窗口办理完业务的时间
      if(window[tempindex]<=custom[i].come){
        window[tempindex] = custom[i].come+custom[i].time;
      }else{
        result +=(window[tempindex]-custom[i].come);
        window[tempindex] +=custom[i].time;
      }
    }
    if(custom.size()==0){
        printf("0.0");

    }else{
        printf("%.1f",result/60.0/custom.size());
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值