标签:des blog io ar os sp for div on
Description
Input
Output
Sample Input
Sample Output
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxm=50001; const int maxn=51; struct edge{ int to,cost,nxt; }e[maxm]; int first[maxn],price[maxn],m,n,len; void addedge(int f,int t,int c){ e[len].to=t;e[len].cost=c;e[len].nxt=first[f]; first[f]=len++; } void debug(){ for(int i=1;i<=n;i++){ for(int p=first[i];p!=-1;p=e[p].nxt){ int t=e[p].to; printf("f %d t %d c %d\n",i,t,e[p].cost); } } } queue<int>que; void spfa(){ for(int i=1;i<=n;i++)que.push(i); while(!que.empty()){ int f=que.front();que.pop(); for(int p=first[f];p!=-1;p=e[p].nxt){ int t=e[p].to; if(price[t]>price[f]+e[p].cost){ price[t]=price[f]+e[p].cost; que.push(t); } } } } int main(){ int caT; scanf("%d",&caT); while(caT--){ memset(first,-1,sizeof(first)); memset(price,0x3f,sizeof(price)); scanf("%d",&n); int f,t,c; len=0; for(int i=0;i<n;i++){scanf("%d%d",&f,&c);price[f]=c;} scanf("%d",&m); for(int i=0;i<m;i++){scanf("%d%d%d",&f,&t,&c);if(f!=t)addedge(f,t,c);} for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(i!=j&&price[i]==price[j]){addedge(j,i,price[i]-price[j]);addedge(i,j,price[i]-price[j]);} for(int i=1;i<=n;i++)if(price[i]>0)price[i]--; //debug(); spfa(); for(int i=1;i<=n;i++)printf("%d %d\n",i,price[i]); int ans=0; for(int i=1;i<=n;i++){ bool fl=false; for(int j=1;j<=n;j++){ for(int k=j+1;k<=n;k++){ if(i!=j&&i!=k&&price[j]+price[k]==price[i]){fl=true;break;} } if(fl)break; } if(fl)ans++; } printf("%d\n",ans); } return 0; }
hdu 3268 09 宁波 现场 I - Columbus’s bargain
标签:des blog io ar os sp for div on
原文地址:http://www.cnblogs.com/xuesu/p/4089976.html