基础:
HDU1596
HDU2112
HDU1874
HDU1869
HDU2066
HDU2094
HDU2544
稍加复杂:
HDU1217 顺练习map离散 难度1.5
HDU1245 处理起点,终点 难度2.5
HDU1535 2次迪杰斯特拉 难度2
HDU2170 难度x
HDU3631 难度x
HDU4284 BFS+floyd 难度2.5
加深对k的理解:
HDU1599(最小环)
ZOJ3710
判环:
HDU1317
暴力搜索+floyd
HDU1217
#include<cstdlib>
#include<iostream>
#include<map>
using namespace std;
int n,m,Case=0;
map<string,int>q;
double Map[100][100],tmp;
string s,s1,s2;
int main()
{
int i,j,k;
bool flag;
while(~scanf("%d",&n)){
if(n<=0) return 0;
q.clear();
for(i=1;i<=n;i++){
cin>>s;
q[s]=i;
}
cin>>m;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i==j) Map[i][j]=1;
else Map[i][j]=0;
for(i=1;i<=m;i++){
cin>>s1>>tmp>>s2;
Map[q[s1]][q[s2]]=tmp;
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(Map[i][j]<Map[i][k]*Map[k][j])
Map[i][j]=Map[i][k]*Map[k][j];
flag=false;
for(i=1;i<=n;i++)
if(Map[i][i]>1) {flag=true;break;}
printf("Case %d: ",++Case);
if(flag) printf("Yes
");
else printf("No
");
}
return 0;
}
HDU1245
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<memory.h>
#include<algorithm>
#include<cmath>
using namespace std;
const double inf=100000000;
int n;
double Map[110][110],x[110],y[110],d;
int step[110][110];
int main()
{
int i,j,k;
while(~scanf("%d%lf",&n,&d)){
n++;
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
{
Map[i][j]=inf;
step[i][j]=inf;
}
for(i=1;i<n;i++)
{
cin>>x[i]>>y[i];
double tmp=sqrt((x[i]*x[i])+(y[i]*y[i]));
if(tmp-7.5<=d&&tmp>7.5){//不加tmp>7.5就错了,因为会出现负数Map
Map[0][i]=tmp-7.5;
step[0][i]=1;
}
}
if(d>=42.5){
Map[0][n]=42.50;
step[0][n]=1;
}
for(i=1;i<n;i++)
for(j=i+1;j<n;j++)
{
double tmp=sqrt(((x[i]-x[j])*(x[i]-x[j]))+((y[i]-y[j])*(y[i]-y[j])));
if(tmp<=d){
Map[i][j]=tmp;
step[i][j]=1;
Map[j][i]=tmp;
step[j][i]=1;
}
}
for(i=1;i<n;i++)
{
double tmp=min(abs(50-x[i]),abs(50+x[i]));
tmp=min(tmp,abs(50-y[i]));
tmp=min(tmp,abs(50+y[i]));
if(tmp<=d){
Map[i][n]=tmp;
step[i][n]=1;
}
}
for(k=0;k<=n;k++)
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
{
if(Map[i][j]>Map[i][k]+Map[k][j]){
Map[i][j]=Map[i][k]+Map[k][j];
step[i][j]=step[i][k]+step[k][j];
}
else if(Map[i][j]==Map[i][k]+Map[k][j])
step[i][j]=min(step[i][j],step[i][k]+step[k][j]);
}
if(Map[0][n]==inf) printf("can't be saved
");
else printf("%.2lf %d
",Map[0][n],step[0][n]);
}
return 0;
}
HDU1596
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<memory.h>
using namespace std;
double Map[1010][1010];
int main()
{
int n,i,j,k,m,a,b;
while(~scanf("%d",&n)){
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%lf",&Map[i][j]);
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(Map[i][j]<Map[i][k]*Map[k][j])
Map[i][j]=Map[i][k]*Map[k][j];
scanf("%d",&m);
while(m--){
scanf("%d%d",&a,&b);
if(Map[a][b]==0) printf("What a pity!
");
else printf("%.3lf
",Map[a][b]);
}
}
return 0;
}
HDU1535
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<memory.h>
#include<algorithm>
#include<queue>
using namespace std;
const int inf=1000000000;
const int Maxn=1000010;
const int Maxm=10000010;
int dis1[Maxn],Laxt1[Maxn],len1[Maxm],To1[Maxm],Next1[Maxm];
int dis2[Maxn],Laxt2[Maxn],len2[Maxm],To2[Maxm],Next2[Maxm];
int n,m,cnt1,cnt2;
queue<int>q1;
queue<int>q2;
bool w1[Maxn];
bool w2[Maxn];
void _update()
{
dis1[1]=0;
for(int i=2;i<=n;i++) dis1[i]=inf;
memset(Laxt1,0,sizeof(Laxt1));
memset(w1,0,sizeof(w1));
cnt1=1;
dis2[1]=0;
for(int i=2;i<=n;i++) dis2[i]=inf;
memset(Laxt2,0,sizeof(Laxt2));
memset(w2,0,sizeof(w2));
cnt2=1;
}
void _add(int u,int v,int d)
{
Next1[++cnt1]=Laxt1[u];
Laxt1[u]=cnt1;
To1[cnt1]=v;
len1[cnt1]=d;
Next2[++cnt2]=Laxt2[v];
Laxt2[v]=cnt2;
To2[cnt2]=u;
len2[cnt2]=d;
}
void _init()
{
int i,x,y,z;
for(i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
_add(x,y,z);
}
}
void _dij1()
{
q1.push(1);
w1[1]=true;
while(!q1.empty()){
int s=q1.front();
q1.pop();
w1[s]=false;
for(int i=Laxt1[s];i>0;i=Next1[i]){
if(dis1[To1[i]]>dis1[s]+len1[i]){
dis1[To1[i]]=dis1[s]+len1[i];
if(!w1[To1[i]]){
w1[To1[i]]=true;
q1.push(To1[i]);
}
}
}
}
}
void _dij2()
{
q2.push(1);
w2[1]=true;
while(!q2.empty()){
int s=q2.front();
q2.pop();
w2[s]=false;
for(int i=Laxt2[s];i>0;i=Next2[i]){
if(dis2[To2[i]]>dis2[s]+len2[i]){
dis2[To2[i]]=dis2[s]+len2[i];
if(!w2[To2[i]]){
w2[To2[i]]=true;
q2.push(To2[i]);
}
}
}
}
}
int main()
{
int T,i,j,k;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
_update();
_init();
_dij1();
_dij2();
int ans=0;
for(i=1;i<=n;i++)
ans+=dis1[i]+dis2[i];
cout<<ans<<endl;
}
return 0;
}
HDU1869
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<string>
#include<memory.h>
using namespace std;
const int inf=1000000000;
int Map[250][250],m,n;
int k,i,j;
int x,y,z,s,t;
int main()
{
while(~scanf("%d%d",&n,&m)){
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i!=j) Map[i][j]=inf;
for(i=1;i<=m;i++){
scanf("%d%d",&x,&y);
Map[x][y]=1;
Map[y][x]=1;
}
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(Map[i][j]>Map[i][k]+Map[k][j])
Map[i][j]=Map[i][k]+Map[k][j];
bool flag=true;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(Map[i][j]>7) { flag=false;break;}
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
hdu1599
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<string>
#include<memory.h>
using namespace std;
const int inf=100000000;
int Map[250][250],mp[250][250],m,n;
int k,i,j;
int x,y,z,s,t;
int main()
{
while(~scanf("%d%d",&n,&m)){
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
Map[i][j]=inf;
for(i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
if(Map[x][y]>z) Map[x][y]=z;
if(Map[y][x]>z) Map[y][x]=z;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
mp[i][j]=Map[i][j];
int ans=inf;
for(k=1;k<=n;k++){
for(i=1;i<k;i++)
for(j=i+1;j<k;j++)
if(mp[i][k]+mp[j][k]+Map[i][j]<ans)
ans=mp[i][k]+mp[j][k]+Map[i][j];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(Map[i][j]>Map[i][k]+Map[k][j])
Map[i][j]=Map[i][k]+Map[k][j];
}
if(ans>=inf) cout<<"It's impossible."<<endl;
else cout<<ans<<endl;
}
return 0;
}
HDU1874
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<string>
#include<memory.h>
using namespace std;
const int inf=1000000000;
int Map[250][250],m,n;
int k,i,j;
int x,y,z,s,t;
int main()
{
while(~scanf("%d%d",&n,&m)){
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i!=j) Map[i][j]=inf;
for(i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
if(Map[x][y]>z) Map[x][y]=z;
if(Map[y][x]>z) Map[y][x]=z;
}
scanf("%d%d",&s,&t);
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(Map[i][j]>Map[i][k]+Map[k][j])
Map[i][j]=Map[i][k]+Map[k][j];
if(Map[s][t]==inf) cout<<"-1"<<endl;
else cout<<Map[s][t]<<endl;
}
return 0;
}
hdu2094
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<memory.h>
using namespace std;
map<string,int>q;
int Map[1000][1000];
int main(){
int n,cnt,i,k,j;
string s1,s2;
while(~scanf("%d",&n)){
if(n==0) return 0;
cnt=0;
q.clear();
memset(Map,0,sizeof(Map));
for(i=1;i<=n;i++){
cin>>s1>>s2;
if(q.find(s1)==q.end()) q[s1]=++cnt;
if(q.find(s2)==q.end()) q[s2]=++cnt;
Map[q[s1]][q[s2]]=1;
}
for(k=1;k<=cnt;k++)
for(i=1;i<=cnt;i++)
for(j=1;j<=cnt;j++)
if(Map[i][k]==1&&Map[k][j]==1) Map[i][j]=1;
bool ok,over=false;
for(i=1;i<=cnt;i++){
ok=true;
for(j=1;j<=cnt;j++){
if(i==j) continue;
if(Map[i][j]!=1) ok=false;
if(Map[j][i]==1) ok=false;
}
if(ok) {
printf("Yes
");
over=true;
break;
}
}
if(!over) printf("No
");
}
return 0;
}
HDU2112
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<map>
#include<cstring>
#include<string>
#include<memory.h>
using namespace std;
const long long Inf=100000000000000000;
int n,i,j,k,cnt;
long long d;
map<string,int>q;
long long m[160][160];
string s1,t1,x,y;
int main()
{
while(~scanf("%d",&n)){
if(n==-1) return 0;
cnt=0;
for(i=1;i<=150;i++)
for(j=1;j<=150;j++)
if(i==j) m[i][j]=0;
else m[i][j]=Inf;
q.clear();
cin>>s1>>t1;
for(i=1;i<=n;i++){
cin>>x>>y>>d;
if(q.find(x)==q.end()) q[x]=++cnt;
if(q.find(y)==q.end()) q[y]=++cnt;
if(m[q[x]][q[y]]>d) m[q[x]][q[y]]=d;
if(m[q[y]][q[x]]>d) m[q[y]][q[x]]=d;
}
for(k=1;k<=cnt;k++)
for(i=1;i<=cnt;i++)
for(j=1;j<=cnt;j++)
if(m[i][k]+m[k][j]<m[i][j]) m[i][j]=m[i][k]+m[k][j];
if(q[s1]>=1&&q[t1]>=1&&m[q[s1]][q[t1]]!=Inf) cout<<m[q[s1]][q[t1]]<<endl;
else cout<<"-1"<<endl;
}
return 0;
HDU2066
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<memory.h>
#include<queue>
using namespace std;
queue<int>q;
const int Inf=0x3f3f3f3f;;
const int Maxm=3000100;
const int Maxn=1010;
int dis[Maxn],cnt,n,Min,c,dr;
int Laxt[Maxm],Next[Maxm],To[Maxm],Len[Maxm];
int conti[Maxn],dream[Maxn];
int Win[Maxn];
void _add(int u,int v,int d){
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
Len[cnt]=d;
Next[++cnt]=Laxt[v];
Laxt[v]=cnt;
To[cnt]=u;
Len[cnt]=d;
}
void _dij()
{
while(!q.empty()){
int s=q.front();
q.pop();
Win[s]=0;
for(int i=Laxt[s];i>0;i=Next[i]){
if(dis[s]+Len[i]<dis[To[i]])
{
dis[To[i]]=dis[s]+Len[i];
if(!Win[To[i]]) {
q.push(To[i]);
Win[To[i]]=1;
}
}
}
}
}
int main()
{
int m,i,j,k,x,y,z;
while(cin>>m>>c>>dr)
{
memset(Laxt,0,sizeof(Laxt));
memset(Next,0,sizeof(Next));
memset(Win,0,sizeof(Win));
cnt=1;
for(i=1;i<=m;i++) {
scanf("%d%d%d",&x,&y,&z);
_add(x,y,z);
dis[x]=Inf;
dis[y]=Inf;
}
for(i=1;i<=c;i++) {
scanf("%d",&conti[i]);
dis[conti[i]]=0;
q.push(conti[i]);
Win[conti[i]]=1;
}
for(i=1;i<=dr;i++) {
scanf("%d",&dream[i]);
dis[dream[i]]=Inf;
}
_dij();
Min=Inf;
for(j=1;j<=dr;j++)
if(Min>dis[dream[j]]) {
Min=dis[dream[j]];
}
printf("%d
",Min);
}
return 0;
}
HDU4284
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<memory.h>
#include<algorithm>
using namespace std;
const int inf=0x3fffffff;
int Map[110][110];
int City[20],Ci[20],Di[20];
int n,m,Mon,H,T;
int used[120];
bool ok;
void _tempt(int u,int k,int tmp)
{
if(k==H)
if(tmp>=Map[u][1]){
ok=true;
return ;
}
for(int i=1;i<=H;i++){
if(used[City[i]]!=T&&Map[u][City[i]]!=inf){
if(tmp-Map[u][City[i]]>=Di[i])
{
used[City[i]]=T;
_tempt(City[i],k+1,tmp-Map[u][City[i]]-Di[i]+Ci[i]);
}
if(ok) return ;
used[City[i]]=0;
}
}
}
int main()
{
int x,y,z,i,j,k;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&Mon);
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
if(i==j) Map[i][j]=0;
else Map[i][j]=inf;
for(i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
if(z<Map[x][y]) Map[x][y]=z;
if(z<Map[y][x]) Map[y][x]=z;
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(Map[i][j]>Map[i][k]+Map[k][j])
Map[i][j]=Map[i][k]+Map[k][j];
scanf("%d",&H);
for(i=1;i<=H;i++)
scanf("%d%d%d",&City[i],&Ci[i],&Di[i]);
ok=false;
_tempt(1,0,Mon);
if(ok) printf("YES
");
else printf("NO
");
}
return 0;
}