CSP稀疏向量python

提交了n次,还是不行啊~

"60!!"
n,a,b=input().split()
n,a,b=int(n),int(a),int(b)
Index1=set()
Value1={
     }
# endtime = datetime.datetime.now()
for i in range(2,a+2):
    index1,value1=input().split()
    index1, value1=int(index1),int(value1)
    Index1.add(index1)
    Value1[index1]=value1
Index2=set()
Value2={
     }
res=0
for j in range(a+2,a+b+2):
    index2, value2 = input().split()
    index2, value2 = int(index2), int(value2)
    Value2[index2]=value2
    if index2 in Index1:
        res+=Value1[index2]*Value2[index2]
print(res)
"""
60分
"""
n,a,b=map(int,input().split())
Index1=set()
Value1={
     }
for i in range(2,a+2):
    index1,value1=(map(int,input().split()))
    Index1.add(index1)
    Value1[index1]=value1
Index2=set()
Value2={
     }
for j in range(a+2,a+b+2):
    index2, value2 = (map(int, input().split()))
    Index2.add(index2)
    Value2[index2]=value2
I=Index1&Index2
res=0
for i in I:
        res+=Value1[i]*Value2[i]
print(res)
"""
30分!
"""
n,a,b=map(int,input().split())
Index1=[]
Value1=[0]*(n+1)
for i in range(2,a+2):
    index1,value1=input().split()
    index1, value1=int(index1),int(value1)
    Index1.append(index1)
    Value1[index1]=value1
Index2=[]
Value2=[0]*(n+1)
for j in range(a+2,a+b+2):
    index2, value2 = input().split()
    index2, value2 = int(index2), int(value2)
    Index2.append(index2)
    Value2[index2]=value2
I=[]
for k in Index1:
    for i in Index2:
        if k==i:
            I.append(i)
res=0
for i in I:
        res+=Value1[i]*Value2[i]
print(res)
"""
超时!0分!
"""
# import datetime
# starttime = datetime.datetime.now()
n,a,b=map(int,input().split())
Value1=[0]*(n+1)
for i in range(2,a+2):
    index1,value1=map(int,(input().split()))
    # Index1.append(index1)
    Value1[index1]=value1
Value2=[0]*(n+1)
for j in range(a+2,a+b+2):
    index2, value2 = map(int,input().split())
    # Index2.append(index2)
    Value2[index2]=value2
# endtime = datetime.datetime.now()
res=0
for index1, value1 in enumerate(Value1):
    for index2, value2 in enumerate(Value2):
        if index1==index2:
            res += Value1[index1] * Value2[index2]
print(res)
# print (endtime - starttime)

你可能感兴趣的:(python,算法,csp)