# PAT准备之2018.7.20

1001 A+B Format (20)（20 分）

#include<bits/stdc++.h>

using namespace std;

int main(void){
int a,b;
cin >> a >> b;
int c = a+b;
bool isok = true;
if(c < 0){
isok = false;
c = -c;
}
stringstream os;
os << c;
string temp;
os >> temp;
string res = "";
int Count = 0;
for(int i=temp.length()-1;i >= 0;--i){
res += temp[i];
Count++;
if(Count % 3 == 0 && i != 0){
res += ',';
}
}
reverse(res.begin(),res.end());
if(!isok){
cout << '-';
}
cout << res << endl;
return 0;
}


1002 A+B for Polynomials (25)（25 分）

#include<bits/stdc++.h>

using namespace std;
const int MAX = 1010;
map<int,double> mp;
int main(void){
int K,a;
double b;
for(int line = 1;line <= 2;++line){
cin >> K;
for(int i=1;i<=K;++i){
cin >> a >> b;
mp[a] += b;
}
}
int sum = 0;
for(map<int,double>::iterator it = mp.begin();it != mp.end();++it){
if(it->second != 0)
sum++;
}
cout << sum;
for(map<int,double>::reverse_iterator it = mp.rbegin();it != mp.rend();++it){
if(it->second != 0){
cout << " " << it->first << " ";
cout << fixed << setprecision(1) << it->second;
}
}
cout << endl;
return 0;
}


1003 Emergency (25)（25 分）

#include<bits/stdc++.h>

using namespace std;
const int MAX = 550;
const int INF = 0x7fffffff;
struct Node{
int to;
int len;
Node();
Node(int _to,int _len);
};
vector<Node> G[MAX];
int N,M,Begin,To;
int num[MAX],Dis[MAX],path[MAX],sum[MAX];
bool used[MAX];
void Dijkstra(){
fill(Dis,Dis+N,INF);
memset(used,false,sizeof(used));
memset(sum,0,sizeof(sum));
Dis[Begin] = 0;
path[Begin] = 1;
sum[Begin] = num[Begin];
while(true){
int u=-1;
for(int i=0;i<N;++i){
if(!used[i] && (u == -1 || Dis[i] < Dis[u] || (Dis[i] == Dis[u] && sum[i] > sum[u])))
u = i;
}
if(u == -1 || u == To) break;
used[u] = true;
for(vector<Node>::iterator it =G[u].begin();it != G[u].end();++it){
int to = it->to;
int len = it->len;
if(used[to])    continue;
if(Dis[to] > Dis[u] + len){
Dis[to] = Dis[u] + len;
sum[to] = sum[u] + num[to];
path[to] = path[u];
}
else if(Dis[to] == Dis[u] + len){
path[to] += path[u];
if(sum[to] < sum[u] + num[to]){
sum[to] = sum[u] + num[to];
}
}
}
}
}
int main(void){
cin >> N >> M >> Begin >> To;
int u,v,w;
for(int i=0;i<N;++i){
cin >> num[i];
}
for(int i=1;i<=M;++i){
cin >> u >> v >> w;
G[u].push_back(Node(v,w));
G[v].push_back(Node(u,w));
}
Dijkstra();
cout << path[To] << " " << sum[To] << endl;
return 0;
}

Node::Node(){
to = len = 0;
}
Node::Node(int _to,int _len){
to = _to;
len = _len;
}