接上篇:
- int dis[101];
- bool iset[101];
- struct Node
- {
- int nextNode;
- int cost;
- };
- int main126()
- {
- vector<Node> node[101];
- int n,m;
- while(cin>>n>>m)
- {
- if(n==0 && m==0) break;
- int i,j;
- for(i=0; i<101; ++i)
- {
- node[i].clear();
- dis[i] = 0XFFFFFFF;
- iset[i] = false;
- }
- for(i=0; i<m; ++i)
- {
- int a,b,c;
- cin>>a>>b>>c;
- Node no;
- no.nextNode = b;
- no.cost = c;
- node[a].push_back(no);
- no.nextNode = a;
- node[b].push_back(no);
- }
- int begin = 1;
- dis[begin] = 0;
- for(j=1; j<n; ++j)
- {
- iset[begin] = true;
- for(i=0; i<node[begin].size(); ++i) //更新dis[]距离
- {
- if(!iset[node[begin][i].nextNode] && (dis[begin] + node[begin][i].cost < dis[node[begin][i].nextNode]))
- {
- dis[node[begin][i].nextNode] = dis[begin] + node[begin][i].cost;
- }
- }
- int min = 0XFFFFFFF;
- for(i=1; i<=n; ++i) //找到最小距离的点
- {
- if(!iset[i] && min > dis[i])
- {
- min = dis[i];
- begin = i;
- }
- }
- }
- cout<<dis[n]<<endl;
- }
- return 0;
- }
- struct Node2
- {
- int nextNode,len,cost;
- };
- int main127()
- {
- int n,m;
- while(cin>>n>>m)
- {
- vector<Node2> vn[101];
- if(n==0 && m==0)
- break;
- int i,j ;
- for(i=0; i<m; ++i)
- {
- int a,b,d,p;
- cin>>a>>b>>d>>p;
- Node2 no;
- no.nextNode = b;
- no.len = d;
- no.cost = p;
- vn[a].push_back(no);
- no.nextNode = a;
- vn[b].push_back(no);
- }
- int dis[101];
- int cst[101];
- bool iset[101];
- for(i=0; i<101; ++i)
- {
- dis[i] = 0XFFFFFFF;
- iset[i] = false;
- cst[i] = 0;
- }
- int s,t;
- cin>>s>>t;
- dis[s] = 0;
- for(j=1; j<n; ++j)
- {
- iset[s] = true;
- for(i=0; i<vn[s].size(); ++i)
- {
- if(!iset[vn[s][i].nextNode] && (dis[s]+vn[s][i].len < dis[vn[s][i].nextNode] || (dis[s] + vn[s][i].len == dis[vn[s][i].nextNode] && cst[s] + vn[s][i].cost < cst[vn[s][i].nextNode])))
- {
- dis[vn[s][i].nextNode] = dis[s] + vn[s][i].len;
- cst[vn[s][i].nextNode] = cst[s] + vn[s][i].cost;
- }
- }
- int min = 0XFFFFFFF;
- for(i=1; i<=n; ++i)
- {
- if(!iset[i] && min > dis[i])
- {
- min = dis[i];
- s = i;
- }
- }
- }
- cout<<dis[t]<<" "<<cst[t]<<endl;
- }
- return 0;
- }
- int s3,n3,m3; ///每个杯子的空体积
- struct Cup
- {
- int ss,nn,mm;
- int t;
- Cup(int s1,int n1,int m1,int t1)
- {
- ss = s1; nn = n1; mm = m1; t = t1;
- }
- };
- void AtoB3(int a,int &sa,int b,int &sb) //把第一个杯子中的水倒入第二个杯子中
- {
- if(b-sb >= sa)
- {
- sb = sb + sa;
- sa = 0; //这两步不能颠倒
- }else
- {
- sa -= (b-sb);
- sb = b;
- }
- }
- queue<Cup> qc;
- bool mark4[101][101][101];
- int BFS3()
- {
- while(!qc.empty())
- {
- Cup cup = qc.front();
- qc.pop();
- // cout<<"Form : "<<cup.ss<<" "<<cup.nn<<" "<<cup.mm<<endl;
- int ss = cup.ss;
- int nn = cup.nn;
- int mm = cup.mm;
- mark4[ss][nn][mm] = true;
- AtoB3(s3,ss,n3,nn); //s->n
- // cout<<ss<<" "<<nn<<" "<<mm<<endl;
- if(!mark4[ss][nn][mm] ) //通过记录状态来确定是否添加新节点
- {
- qc.push(Cup(ss,nn,mm,cup.t+1));
- mark4[ss][nn][mm] = true;
- if(ss == s3/2 && nn == s3/2) return cup.t +1 ;
- if(nn == s3/2 && mm == s3/2) return cup.t +1 ;
- if(ss == s3/2 && mm == s3/2) return cup.t +1 ;
- }
- ss = cup.ss;
- nn = cup.nn;
- mm = cup.mm;
- // cout<<"Form : "<< ss<<" "<< nn<<" "<< mm<<endl;
- AtoB3(n3,nn,s3,ss); //n->s
- // cout<<ss<<" "<<nn<<" "<<mm<<endl; system("pause");
- if(!mark4[ss][nn][mm] )
- {
- qc.push(Cup(ss,nn,mm,cup.t+1));
- mark4[ss][nn][mm] = true;
- if(ss == s3/2 && nn == s3/2) return cup.t +1 ;
- if(nn == s3/2 && mm == s3/2) return cup.t +1 ;
- if(ss == s3/2 && mm == s3/2) return cup.t +1 ;
- }
- ss = cup.ss;
- nn = cup.nn;
- mm = cup.mm;
- AtoB3(s3,ss,m3,mm); //s->m
- // cout<<ss<<" "<<nn<<" "<<mm<<endl;
- if(!mark4[ss][nn][mm] )
- {
- qc.push(Cup(ss,nn,mm,cup.t+1));
- mark4[ss][nn][mm] = true;
- if(ss == s3/2 && nn == s3/2) return cup.t +1 ;
- if(nn == s3/2 && mm == s3/2) return cup.t +1 ;
- if(ss == s3/2 && mm == s3/2) return cup.t +1 ;
- }
- ss = cup.ss;
- nn = cup.nn;
- mm = cup.mm;
- AtoB3(m3,mm,s3,ss); //m->s
- // cout<<ss<<" "<<nn<<" "<<mm<<endl;
- if(!mark4[ss][nn][mm] )
- {
- qc.push(Cup(ss,nn,mm,cup.t+1));
- mark4[ss][nn][mm] = true;
- if(ss == s3/2 && nn == s3/2) return cup.t +1 ;
- if(nn == s3/2 && mm == s3/2) return cup.t +1 ;
- if(ss == s3/2 && mm == s3/2) return cup.t +1 ;
- }
- ss = cup.ss;
- nn = cup.nn;
- mm = cup.mm;
- AtoB3(n3,nn,m3,mm); //n->m
- // cout<<ss<<" "<<nn<<" "<<mm<<endl;
- if(!mark4[ss][nn][mm] )
- {
- qc.push(Cup(ss,nn,mm,cup.t+1));
- mark4[ss][nn][mm] = true;
- if(ss == s3/2 && nn == s3/2) return cup.t +1 ;
- if(nn == s3/2 && mm == s3/2) return cup.t +1 ;
- if(ss == s3/2 && mm == s3/2) return cup.t +1 ;
- }
- ss = cup.ss;
- nn = cup.nn;
- mm = cup.mm;
- AtoB3(m3,mm,n3,nn); //m->n
- // cout<<ss<<" "<<nn<<" "<<mm<<endl;
- if(!mark4[ss][nn][mm] )
- { qc.push(Cup(ss,nn,mm,cup.t+1));
- mark4[ss][nn][mm] = true;
- if(ss == s3/2 && nn == s3/2) return cup.t +1 ;
- if(nn == s3/2 && mm == s3/2) return cup.t +1 ;
- if(ss == s3/2 && mm == s3/2) return cup.t +1 ;
- }
- }
- return -1;
- }
- int main128()
- {
- while(cin>>s3>>n3>>m3)
- {
- if(s3==0 && n3==0 && m3==0) break;
- if(s3%2 != 0)
- { cout<<"NO"<<endl; continue;}
- memset(mark4,false,sizeof(mark4));
- while(!qc.empty()) qc.pop();
- Cup cup(s3,0,0,0);
- qc.push(cup);
- int tmp = BFS3();
- if(tmp == -1) cout<<"NO"<<endl;
- else
- cout<<tmp<<endl;
- }
- return 0;
- }
- int circle[17];
- bool isEle[17];
- int size = 1;
- int n4;
- bool flag = false;
- int ca = 1;
- void prime_circle()
- {
- if(size == n4)
- {
- if(isPrime(circle[1]+circle[size]))
- {
- if(flag)
- {
- cout<<"Case "<<ca<<":"<<endl;
- flag = false;
- }
- for(int i=1; i<=size; ++i)
- cout<<circle[i]<<" ";
- cout<<endl;
- }
- return;
- }
- for(int i=1; i<=n4; ++i)
- {
- if(!isEle[i])
- {
- if(isPrime(circle[size]+i))
- {
- circle[++size] = i;
- isEle[i] = true;
- prime_circle();
- --size;
- isEle[i] = false;
- }
- }
- }
- }
- int main129()
- {
- circle[size] = 1;
- memset(isEle,false,sizeof(isEle));
- isEle[1] = true;
- while(cin>>n4)
- {
- flag = true;
- prime_circle();
- ++ca;
- cout<<endl;
- }
- return 0;
- }
- int checks[101];
- int dp5[10000001];
- int main130()
- {
- double dmount,n;
- int imount,size;
- while(cin>>dmount>>n && n)
- {
- for(int i=0; i<101; ++i) checks[i] = 0;
- imount = (int)(100*dmount);
- for(i=0; i<=imount; ++i) dp5[i] = 0;
- size = 0;
- while(n--)
- {
- bool flag = true;
- int m;
- cin>>m;
- for(int j=0; j<m; ++j)
- {
- double dtmp;
- char str[80];
- //scanf(" %c:%d",&ch,&dtmp);
- //cin>>ch>>":">>dtmp;
- cin>>str;
- int k=0;
- while(str[k] == ' ') ++k;
- char ch = str[k];
- k+=2;
- dtmp = atof(str+k);
- // cout<<"cout: "<<ch<<" "<<dtmp<<endl;
- if(ch != 'A' && ch != 'B' && ch != 'C')
- { flag = false; }
- else
- {
- if(dtmp <= 600)
- checks[size] += (int)(dtmp*100);
- else flag = false;
- }
- }
- if(flag)
- {
- if(checks[size] <= 100000)
- ++size;
- }
- }
- // for(i=0; i<size; ++i)
- // cout<<checks[i]<<endl;
- for(i=0; i<size; ++i)
- {
- for(int j=imount; j>=checks[i]; --j)
- dp5[j] = max(dp5[j],dp5[j-checks[i]]+checks[i]);
- }
- cout<<fixed<<setprecision(2)<<((double)dp5[imount])/100<<endl;
- }
- return 0;
- }
- int main131() //获取所有字符子串的个数,利用映射
- {
- string str;
- while(cin>>str)
- {
- map<string,int> msi;
- for(int i=0; i<str.length(); ++i)
- {
- for(int j=1; j+i<=str.length(); ++j)
- {
- string ss = str.substr(i,j);
- if(msi.find(ss) != msi.end())
- ++msi[ss];
- else msi[ss] = 1;
- }
- }
- map<string,int>::iterator b = msi.begin();
- map<string,int>::iterator e = msi.end();
- while(b!=e)
- {
- if(b->second > 1)
- {
- cout<<b->first<<" "<<b->second<<endl;
- }
- ++b;
- }
- }
- return 0;
- }
- void rol(char *str,int i)
- {
- int len = strlen(str);
- str[len] = str[i];
- }
- int main132()
- {
- int n;
- while(cin>>n)
- {
- while(n--)
- {
- char s1[33],s2[17];
- for(int i=0; i<16; ++i)
- {
- s1[i] = '0';
- s2[i] = '0';
- }
- s2[16] = '\0';
- while(i<33) s1[i++] = '\0';
- int a,b;
- cin>>a>>b;
- i=0;
- while(a)
- {
- if(a & 0X8000)
- s1[i] = '1';
- a <<= 1;
- ++i;
- }
- i=0;
- while(b)
- {
- if(b & 0X8000)
- s2[i] = '1';
- b <<= 1;
- ++i;
- }
- // cout<<s1<<endl<<s2<<endl;
- for(i=0;i<15; ++i)
- {
- if(strcmp(s1+i,s2) == 0)
- break;
- rol(s1,i);
- }
- if(i==15)
- cout<<"NO"<<endl;
- else cout<<"YES"<<endl;
- }
- }
- return 0;
- }
- int dp6[101][101];
- int main140()
- {
- char str1[80],str2[80];
- while(cin>>str1+1>>str2+1)
- {
- int i,j ;
- for(i=0; i<101; ++i)
- {
- dp6[0][i] = 0;
- dp6[i][0] = 0;
- }
- int l1 = strlen(str1+1);
- int l2 = strlen(str2+1);
- // cout<<l1<<" "<<l2<<endl;
- for(i=1; i<=l1; ++i) //
- {
- for(j=1; j<=l2; ++j)
- {
- if(str1[i] == str2[j])
- dp6[i][j] = 1+dp6[i-1][j-1];
- else
- dp6[i][j] = max(dp6[i-1][j],dp6[i][j-1]);
- }
- }
- cout<<dp6[l1][l2]<<endl;
- }
- return 0;
- }
- int main139()
- {
- char str1[80],str2[80];
- while(cin>>str1>>str2)
- {
- int l1 = strlen(str1);
- int l2 = strlen(str2);
- int i,j;
- for(i=0; i<101; ++i)
- {
- dp6[0][i] = 0;
- dp6[i][0] = 0;
- }
- for(i=1; i<=l1; ++i)
- {
- for(j=1; j<=l2; ++j)
- {
- if(str1[i-1] == str2[j-1])
- dp6[i][j] = dp6[i-1][j-1]+1;
- else dp6[i][j] = max(dp6[i-1][j],dp6[i][j-1]);
- }
- }
- cout<<dp6[l1][l2]<<endl;
- }
- return 0;
- }
- /*
- int main()
- {
- int list6[101];
- int i,j,n,k;
- while(cin>>n>>k)
- {
- for(i=0; i<101; ++i)
- dp6[0][i] = 0; //此处的dp6[][]表示前j件东西的i件配对的最小值
- for(i=1; i<=n; ++i)
- cin>>list6[i];
- sort(list6+1,list6+1+n);
- for(i=1; i<=k; ++i)
- {
- for(j=2*i; j<=n; ++j)
- {
- if(j > 2*i)
- dp6[i][j] = dp6[i][j-1]; //如j>2*i的话则表示最后两个可以或者不配对,则先赋值不配对
- else dp6[i][j] = INF; //如最后两个必须配对,那么就把其赋值无穷大
- if(dp6[i][j] > dp6[i-1][j-2] + (list6[j]-list6[j-1]) * (list6[j]-list6[j-1]))
- dp6[i][j] = dp6[i-1][j-2] + (list6[j]-list6[j-1])* (list6[j]-list6[j-1]);
- }
- }
- cout<<dp6[k][n]<<endl;
- }
- return 0;
- }*/
- struct rice
- {
- int price,weight;
- }rices[101];
- int size5;
- int main141() //多重背包
- {
- int C;
- int dp9[10001];
- cin>>C;
- while(C--)
- {
- int n,m,i,j;
- cin>>n>>m;
- size5 = 1;
- int p,h,c,k;
- for(i=1; i<=m; ++i)
- {
- cin>>p>>h>>c;
- k=1;
- while(c-k>0) //对物品进行分解
- {
- rices[size5].price = p*k;
- rices[size5].weight = h*k;
- ++size5;
- c -= k;
- k*=2;
- }
- rices[size5].price = p*c;
- rices[size5].weight = h*c;
- ++size5;
- }
- // for(i=1; i<size5; ++i)
- // cout<<rices[i].price<<" "<<rices[i].weight<<endl;
- for(i=0; i<10001; ++i)
- dp9[i] = 0;
- for(i=1; i<size5; ++i)
- {
- for(j=n; j>=rices[i].price; --j)
- {
- dp9[j] = max(dp9[j], dp9[j-rices[i].price] + rices[i].weight);
- }
- }
- cout<<dp9[n]<<endl;
- }
- return 0;
- }
- bool check(bool que[9][9],int l,int c)
- {
- int i;
- for(i=1; i<l; ++i) //因为是按照行进行递归的所以函数里面此处判断列
- {
- if(que[i][c])
- return false;
- }
- for(i=1; i<l; ++i) //此处判断斜线
- {
- for(int j=1; j<=8; ++j)
- {
- if(que[i][j])
- {
- if(abs(l-i) == abs(j-c))
- return false;
- break;
- }
- }
- }
- return true;
- }
- int cnt;
- void getqueue(bool que[9][9],int line)
- {
- int i;
- for(i=1; i<=8; ++i) //对行line循环列
- {
- if(check(que,line,i))
- {
- if(line == 8)
- ++cnt;
- else
- {
- que[line][i] = true;
- getqueue(que,line+1);
- que[line][i] = false;
- }
- }
- }
- }
- int main142()
- {
- bool que[9][9];
- memset(que,false,sizeof(que));
- cnt = 0;
- getqueue(que,1);
- cout<<cnt<<endl;
- return 0;
- }
- struct ch
- {
- char c[26];
- int index;
- };
- int main()
- {
- int n,m;
- map<char,ch> mcc;
- cin>>n>>m;
- char str[4];
- for(int i=0; i<n; ++i)
- {
- char c1,c2,c3;
- scanf("%s",&str);
- c1 = str[0];
- c2 = str[1];
- c3 = str[2];
- if(mcc.find(c1) != mcc.end())
- {
- if(c2 != '-')
- mcc.find(c1)->second.c[mcc.find(c1)->second.index++] = c2;
- if(c3 != '-')
- mcc.find(c1)->second.c[mcc.find(c1)->second.index++] = c3;
- }else
- {
- struct ch cc;
- cc.index=0;
- if(c2 != '-')
- cc.c[cc.index++] = c2;
- if(c3 != '-')
- cc.c[cc.index++] = c3;
- mcc[c1] = cc;
- }
- }
- /* map<char,ch>::iterator b = mcc.begin();
- map<char,ch>::iterator e = mcc.end();
- while(b!=e)
- {
- cout<<b->first<<" :";
- for(int j=0; j<b->second.index; ++j)
- cout<<b->second.c[j]<<" ";
- ++b;
- }
- cout<<endl;
- */
- for(i=1; i<=m; ++i)
- {
- cin>>str;
- char c1=str[0],c2=str[1];
- }
- return 0;
- }