1. 程式人生 > >浙大PAT甲級 1053

浙大PAT甲級 1053

深度優先搜尋。

可使用鄰接連結串列來儲存整個圖,為了使結果為非增輸出,可根據他們的費用從大到小進行排序,然後從0開始進行深度優先搜尋。陣列path[i]來記錄滿足條件時,i的後續結點。

AC程式碼:

#include<iostream>
#include<vector>
#include<map>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<list>
#include<set>
#include<stack>
#include<cmath>
#define inf 26*26*26*10+5
using namespace std;
int a[105];
vector<int> v[105];
int n,m,s;
int path[105];
bool cmp(int x,int y)
{
    return a[x]>a[y];
}
void dfs(int x,int cost)
{
    if(cost+a[x]==s)
    {
       if(v[x].empty())
       {
           printf("%d",a[0]);
           for(int i=path[0];i!=-1;i=path[i])
           {
               printf(" %d",a[i]);
           }
           printf("\n");
       }
    }
    else if(cost+a[x]<s)
    {
        if(!v[x].empty())
        {
            for(int i=0;i<v[x].size();i++)
            {
                int tmp=path[x];
                path[x]=v[x][i];
                dfs(v[x][i],cost+a[x]);
                path[x]=tmp;
            }
        }
    }
}
int main()
{
    scanf("%d %d %d",&n,&m,&s);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        path[i]=-1;
    }
    for(int i=0;i<m;i++)
    {
        int b,c;
        scanf("%d %d",&b,&c);
        for(int j=0;j<c;j++)
        {
            int d;
            scanf("%d",&d);
            v[b].push_back(d);
        }
        sort(v[b].begin(),v[b].end(),cmp);
    }
    dfs(0,0);
}


相關推薦

浙大PAT甲級 1053

深度優先搜尋。 可使用鄰接連結串列來儲存整個圖,為了使結果為非增輸出,可根據他們的費用從大到小進行排序,然後從0開始進行深度優先搜尋。陣列path[i]來記錄滿足條件時,i的後續結點。 AC程式碼:

浙大pat甲級題目---1032. Sharing (25)

res which mmx suffix ecif 單詞 list title mco 1032. Sharing (25) 時間限制 100 ms 內存限制 65536 kB 代碼長度限制 16000 B 判題程序 Standard 作者 CHEN, Y

浙大pat甲級 1023

用hash來統計每個數的個數,若乘以2以後每個數出現的個數與先前統計的個數一樣,則輸出Yes否則No. #include<iostream> #include<cstring>

* 浙大PAT甲級 1043

二叉查詢樹的建立與數的遍歷。 AC程式碼: #include<iostream> #include<vector> #include<map> #include<cstdio> #include<algorithm>

浙大PAT甲級 1033

貪心題,首先先判斷是否能到達目的地,有兩點: (1)如果在0的位置上沒有加油站,則不能達到目的地,最大距離為0.00。 (2)如果有兩個加油站的距離之差大於cmax*ave或者目的地與最遠加油站的距離也大於cmax*ave,則也不能到達目的地,最大距離為前者加油站的位置+c

浙大PAT甲級-1002

這道題是大一的時候就做過的,當時剛學運算子過載。現在我用的是連結串列來做的,也算是複習一下快忘光的資料結構吧。#include <iostream> #include<iomanip> using namespace std; //節點(多項式的項

浙大 PAT 甲級1009

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 occup

浙大PAT甲級 1073

字串處理,注意如果結果為整數,則不需要小數點,且包含後導0。 AC程式碼: #include<iostream> #include<map> #include<cstdio> #include<algorithm> #incl

** 浙大PAT甲級 1068 01揹包問題

這個題是dp01揹包問題,dp[i][j]表示在j容量下前i個貨幣最大利益,但是難點在如何輸出最小序列。 將輸入的數按遞減順序排序,mark[i][j]==1表示在容量為j時的最大的利益情況下,加入了

*浙大PAT甲級 1081

題目不難,但是需要注意的細節很多,尤其是 long long雖然數的範圍在int內,但是求最小公倍數時,相乘會超出int範圍,因此用long long; AC程式碼: #include<iostream> #include<map> #include

浙大PAT甲級-1016

Phone Bills1.更改檔案輸入輸出方式:freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);******//中間程式碼正常cin,coutfclose(stdin);fclose(stdout

PAT(甲級)1053

Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of a path from R to L is defined to be the sum of the we

浙大PAT甲級-1013

網上說這是在考查並查集的相關演算法 而我是用dfs暴力解決的(差點超時) int isConnect(){ int repair=0; for(int i=1;i<=n;i++)

浙大PAT甲級 1113

超級簡單。 先排序,再判斷n的奇偶性,奇數輸出1,偶數輸出0,然後求出後半段-前半段即可。 AC程式碼: #include<iostream> #include<map> #i

浙大PAT甲級-1022

關鍵點:(1)cin輸入並沒有將換行符輸入所以後面如果緊接著getline(cin,str)需先輸入完換行符(2)ID列印注意前面補零(%07d)(否則後面兩個測試點過不去)#include <iostream> #include <vector> #

*浙大PAT甲級 1060

判斷兩個數是否相等,這個題需要考慮的內容有很多: 1.若有前導0,則應該去除。 2.若輸入的數小於1,則應判斷小數點移動多少位才為0.d1d2.....d1>0除非d1==0。 3若輸入的的數是個整數無小數點,則對應結果的10的指數應為輸入數(去掉前導0)的位數。 4

PAT甲級1053. Path of Equal Weight (30)

Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of a path from R to L is defined t

浙大pat | 浙大pat 牛客網甲級 1007 Consecutive Factors (20) 數論

題目描述Among all the factors of a positive integer N, there may existseveral consecutive numbers.  Forexample, 630 can be factored as 3*5*6*7

浙大pat | 牛客網甲級 1025 Sort with Swap(0) (25)排序

題目描述Given any permutation of the numbers {0, 1, 2,..., N-1}, it iseasy to sort them in increasing order. But what if Swap(0, *) is the ONL

浙大pat | 浙大pat 牛客網甲級1061 Insert or Merge (25)判斷是插入排序還是歸併排序

According to Wikipedia:Insertion sort iterates, consuming one input element each repetition, andgrowing a sorted output list. Each iterati